1

I have a web API built in .NET 4.0 and I have an issue with a simple GET request. My problem is that I want to return an object who has many properties, some strings, ints and other custom data types and in some cases some of those properties doesn't exist in the databsae so I want it to return an empy object {} and I'm just having null.

Here is some of my code

    <ActionName("index")>
    <HttpGet>
    Public Function ObtenerAsegurado(<FromUri> rut As Integer) As Asegurado

        Dim ws As New Getter2.GetterSoapClient
        Dim aseg As Getter2.AseguradoDTO             

        aseg = ws.ObtenerAsegurado(rut)

        Dim objAsegurado As Asegurado = Convertir.DTOToAsegurado(aseg)

        Return objAsegurado    

    End Function

    Public Shared Function DTOToAsegurado(asegDTO As Getter2.AseguradoDTO) As Asegurado
        Dim aseg As New Asegurado

        If Not asegDTO Is Nothing Then
            ...
            aseg.cuenta = DTOToCuentas(asegDTO.Cuenta)
            ...
        End If
        Return aseg
    End Function

    Private Shared Function DTOToCuentas(cuentaDTO As Getter2.CuentaDTO) As Cuenta
        Dim nuevacuenta As New Cuenta

        If Not cuentaDTO Is Nothing AndAlso Not cuentaDTO.DescBanco Is Nothing Then
            ...
        Else
            nuevacuenta = Nothing
        End If
        Return nuevacuenta
    End Function

As you can see, my action call to another function to make some convertion, and there i return the property of the object as nothing when there isn't present, that becomes null in the http response and I want an empty object instead {} I also tried returning nuevacuenta = New Cuenta but that return an object with all it's properties set to nothing... Please help how can I return empty instead of null?

2
  • When I do that I get a casting error because the Object type can not be converted to the custom Cuenta type Commented Jun 24, 2015 at 1:34
  • What you want is a little non-standard. { } will be equals for an instance with no property (or instance of object type). So if you really want this, I think you should declare property type in view model as 'object'. Commented Jun 26, 2015 at 8:50

2 Answers 2

1

I found that it's doable to convert a null instance to empty object {} in JSON. C# code is there https://gist.github.com/juanonsoftware/7067ce53813201abbdae

        var settings = new JsonSerializerSettings()
        {
            ContractResolver = new NullToEmptyObjectResolver(typeof(Child))
        };
        var str = JsonConvert.SerializeObject(m, settings);



class NullToEmptyObjectResolver : DefaultContractResolver
{
    private readonly Type[] _types;

    public NullToEmptyObjectResolver(params Type[] types)
    {
        _types = types;
    }

    protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
    {
        return type.GetProperties().Select(p =>
        {
            var jp = base.CreateProperty(p, memberSerialization);
            jp.ValueProvider = new NullToEmptyValueProvider(p, _types);
            return jp;
        }).ToList();
    }
}

class NullToEmptyValueProvider : IValueProvider
{
    readonly PropertyInfo _memberInfo;
    private readonly Type[] _types;

    public NullToEmptyValueProvider(PropertyInfo memberInfo, params Type[] types)
    {
        _memberInfo = memberInfo;
        _types = types;
    }

    public object GetValue(object target)
    {
        var result = _memberInfo.GetValue(target);

        if (_types.Contains(_memberInfo.PropertyType) && result == null)
        {
            result = new object();
        }

        return result;
    }

    public void SetValue(object target, object value)
    {
        _memberInfo.SetValue(target, value);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

I'm not sure if this is the problem, but I think it could be the way in which you are testing for nothing?

<ActionName("index")>
<HttpGet>

Public Function ObtenerAsegurado(<FromUri> rut As Integer) As Asegurado
  '
  Dim ws As New Getter2.GetterSoapClient
  Dim aseg As Getter2.AseguradoDTO             
  '
  aseg = ws.ObtenerAsegurado(rut)
  Dim objAsegurado As Asegurado = Convertir.DTOToAsegurado(aseg)
  Return objAsegurado
  '    
End Function
'
Public Shared Function DTOToAsegurado(asegDTO As Getter2.AseguradoDTO) As Asegurado
  '
  Dim aseg As New Asegurado
  If (asegDTO Is Nothing) = False Then
    '...
    aseg.cuenta = DTOToCuentas(asegDTO.Cuenta)
    '...
  End If
  Return aseg
  '
End Function
'
Private Shared Function DTOToCuentas(cuentaDTO As Getter2.CuentaDTO) As Cuenta
  '
  Dim nuevacuenta As New Cuenta
  If (cuentaDTO Is Nothing) = False And (cuentaDTO.DescBanco Is Nothing) = False Then
    '...
  Else
    nuevacuenta = Nothing
  End If
  Return nuevacuenta
  '
End Function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.