0

Is there a way to selectively exclude a field from re-serialization, if it already contains a valid json string?

Here is what I am attempting.

First, I define a class of the result row I recieve from my database.

public class objectToSerialize
{
    public string identifier { get; set; }
    public string jsonPayload { get; set; }

    public objectToSerialize()
    {
        identifier = String.Empty;
        jsonPayload = String.Empty;
    }
}

Then I run my query and add all the results to a list.

List<objectToSerialize> listOfobjectToSerialize = new List<objectToSerialize>();
[...]

while(SqlDataReader.Read())
{
   [...]
   listOfobjectToSerialize.add(objectToSerialize)
}

Finally, I return my list as an APS.NET OkObjectResult, which serializes the list to JSON by default.

[...]
return (ActionResult)new OkObjectResult(listOfobjectToSerialize);

The jsonPayload already contains a json string, that should not be re-serialized again when returning the result.
So far I have deserialized the jsonPayload to an object, which was then serialized fine, but I would like to avoid that unnessesary step.
I have tried using the field attributes [JsonIgnore] and [NonSerialized()] but they remove the field from the result altogether.

Is there a way to include the jsonPayload string as-is?

Here is an example:

objectToSerialize.identifier = 123;
objectToSerialize.jsonPayload = @"
   {
      "ThisIsAKey" : "ThisIsAValue"
   }"

Desired output:

{
   "identifier" : 123,
   "jsonPayload" : {
      "ThisIsAKey" : "ThisIsAValue"
   }
}

Actual output:

{
   "identifier" : 123,
   "jsonPayload" : "{
      \"ThisIsAKey\" : \"ThisIsAValue\"
   }"
}

So far I am trying to write my own custom JsonConverter.

roboblocky
  • 81
  • 5
  • 2
    I don't think `jsonPayload` property is getting re-serialized. What makes you believe that's it's getting re-serialized? – Dimitris Maragkos Sep 12 '22 at 11:45
  • 2
    Could you please provide a sample API response which shows this `string` re-serialization issue? – Peter Csala Sep 12 '22 at 12:42
  • 1
    It doesnt' matter that will serialized again, it will return back after deserialization. You can add some code to exclude it but after this it will be not valid after deserialization. Better to leave as it is. – Serge Sep 12 '22 at 14:57
  • 1
    Hi, I Added an example, also I started an attempt to handle this with a custom JsonConverter. But I am unsure If I will succeed. – roboblocky Sep 12 '22 at 15:12
  • 1
    in .NET 6 and later you can use a custom `JsonConverter` and call `Utf8JsonWriter.WriteRawValue()` to write a JSON string as-is. See [Is there an equivalent for JRaw in System.Text.Json](https://stackoverflow.com/a/70477321/3744182) and [How can I serialize a literal JSON value with System.Text.Json?](https://stackoverflow.com/q/69929260/3744182). In fact these look to be duplicates, agree? – dbc Sep 13 '22 at 17:49

2 Answers2

0

this will not be even compiled

objectToSerialize.jsonPayload = @"
   {
      "ThisIsAKey" : "ThisIsAValue"
   }"

if you want Desired output

objectToSerialize.jsonPayload = new
   {
      ThisIsAKey = "ThisIsAValue"
   };

but as I guess your jsonPayload is a string , so you can only do this

objectToSerialize.jsonPayload = @"{
      ""ThisIsAKey"" : ""ThisIsAValue"" 
      }";

leave like this, otherwise you are just looking for a trouble, you will not be able to deserialize jsonPayload to string when you get your response.

Serge
  • 40,935
  • 4
  • 18
  • 45
  • I see, I was motivated to find a better way than first using Newtonsoft JsonConvert.DeserializeObject() to deserialize the string to an object, then serialize it again when I deliver the OkObjectResult. This seemed unnecessarily inefficient, but for now, I cannot see a better solution. Thank s – roboblocky Sep 12 '22 at 15:58
0

If it is already the JSON, we could give a try to plain/text as the content type of the response.

May be,

Request.Content = "text/plain";

return Content(alreadyCreatedJson);

phusonu
  • 51
  • 7