Is it possible to process data receipt in Camel Casing using Odata?

Asked

Viewed 1,072 times

51

In the method Register class WebApiConfig i have configured a CamelCasePropertyNamesContractResolver

public static void Register(HttpConfiguration config)
{           
    //Resto do código removido para brevidade
    config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
}

This causes the data received (or sent) by JSON to be converted from camelCase (client standard - Javascript) for TitleCase (default that I use in the server properties) and vice versa.

Ex. of JSON that the API receives in a method PATCH normal (without using the Delta<T>)

{
    id: 1,
    nome: "Joaquim",
    idade: 150
}

This is "translated" to

{
    Id: 1,
    Nome: "Joaquim",
    Idade: 150
}

So far so good, the problem is that I have a action PATCH in a ApiController who makes use of the Delta<T> and the requests that are sent to this method end up not going through this "translation".

I read in some places that Odata uses a serializer and an deserializer of his own and this must be the problem.

Method PATCH of ApiController:

[HttpPatch, ResponseType(typeof(void))]
public async Task<IHttpActionResult> Patch(int id, [FromBody] Delta<Entity> changes)
{
    var entity = await _db.Entities.FindAsync(id);
    if (entity == null)        
        return NotFound();

    changes.Patch(entity);
    _db.SaveChanges();

    return StatusCode(HttpStatusCode.NoContent);
}

Is there any way to treat the "Casing" of the data received in actions who use Odata?

Do I need to write a new serializer or do you already have something ready that can help me with that? If you need to write a serializer, you can help me with at least the basic implementation of it?

Example of JSON I get in the com method Delta<T>

{ nome: "Teste método PATCH" }

If I send the JSON, as the below, works normal.

{ Nome: "Teste método PATCH" }

Note: I am using OWIN, but the application is being hosted on IIS (using the package (Microsoft.Owin.Host.SystemWeb).

2 answers

1

I believe this will solve the problem:

Apply this attribute [Camelcasingfilter] to any action you want as Camel Casing.

public class CamelCasingFilterAttribute : ActionFilterAttribute
{
    private JsonMediaTypeFormatter _camelCasingFormatter = new JsonMediaTypeFormatter();

    public CamelCasingFilterAttribute()
    {
        _camelCasingFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
    }

    public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
    {
        ObjectContent content = actionExecutedContext.Response.Content as ObjectContent;
        if (content != null)
        {
            if (content.Formatter is JsonMediaTypeFormatter)
            {
                actionExecutedContext.Response.Content = new ObjectContent(content.ObjectType, content.Value, _camelCasingFormatter);
            }
        }
    }
}

Source: https://stackoverflow.com/questions/14528779/use-camel-case-serialization-only-for-specific-actions?answertab=oldest#tab-top

-1

  • That doesn’t work because the builder is used to serialize objects and my goal is to deserialize a payload.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.