Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON serialized output has C# escaped formatting

I am trying to POST an updated object to REST server API from my client. I am using RestSharp and I am adding JSON representation of my object to the body of my request. However, my string representation of the serialized object has wrong format. The server rejects it.

My request looks something like this (I used Fiddler to get it)

PUT https://myapp.net/api/PriceListItems/151276 HTTP/1.1
Accept: application/json, application/xml, text/json, text/x-json, text/javascript, text/xml
User-Agent: RestSharp/104.4.0.0
Content-Type: application/json
Host: myapp.net
Content-Length: 75
Accept-Encoding: gzip, deflate

"{\"Id\":151276,\"AgendaId\":0,\"CurrencyId\":1,\"Code\":\"\",\"Price\":7.0}"

I have tried to serialize my object with Json.NET, RestSharp's internal Json serializer and with JavaScriptSerializer from System.Web.Script.Serialization. All return a string in such format. I know that the reason for such formatting is because C# escapes double quotes so it can display it properly inside, but i dont understand how am I supposed to pass it to my request without this escaped formatting. I know for a fact that properly formed JSON is accepted by the server.

The object that I am trying to serialize looks like this

public class PriceListItem
    {
        public static PriceListItem CreatePriceListItem(int id, int agendaId, int currencyId, string code, string unit, decimal price)
        {
            var priceListItem = new PriceListItem
            {
                Id = id,
                AgendaId = agendaId,
                CurrencyId = currencyId,
                Code = code,
                Price = price
            };
            return priceListItem;
        }

        public int Id { get; set; }

        public int AgendaId { get; set; }

        public int CurrencyId { get; set; }

        public string Code { get; set; }

        public decimal Price { get; set; }

EDIT: Moved my solution from here to answers.

like image 757
filipv Avatar asked Jul 22 '14 14:07

filipv


People also ask

What can be serialized in JSON?

JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object). If you serialize this result it will generate a text with the structure and the record returned.

What is a JSON serialized object?

Json namespace provides functionality for serializing to and deserializing from JavaScript Object Notation (JSON). Serialization is the process of converting the state of an object, that is, the values of its properties, into a form that can be stored or transmitted.

Is JSON serialize format?

JSON is a ubiquitous human-readable data serialization format that is supported by almost every popular programming language. JSON's data structures closely represent common objects in many languages, e.g. a Python dict can be represented by a JSON object , and a Python list by a JSON array .

What is serialization in C# JSON?

Serialization is the process of converting . NET objects such as strings into a JSON format and deserialization is the process of converting JSON data into . NET objects.


1 Answers

I just read another topic on this problem here. The problem was that I serializing the object twice.

Instead of

request.AddBody(JsonConvert.SerializeObject(priceListItem));

I should have used

request.AddBody(priceListItem);

Anyway, maybe it will help someone else. I find it strange however that the object is serialized automatically.

like image 62
filipv Avatar answered Sep 18 '22 13:09

filipv