André Reichelt
André Reichelt

Reputation: 1631

Raw property with System.Text.Json

I wonder, if there is a way to keep the raw content of one specific property in my JSON object.

public class MyClass {
  public int RegularNumber { get; set; }
  public string OtherStuff { get; set; }
  public RawJsonString SomeNestedData { get; set; }
}

The background is, that the client side SomeNestedData object is an irregular array of strings and arrays. I can't find a proper c# type that would fit and deserializing the property on the server isn't even necessary, because it's getting saved to a database as raw JSON.

So is there some way to achieve something like a RawJsonString class? Or should I better use IEnumerable<object>?

Upvotes: 1

Views: 837

Answers (1)

Lynyrd
Lynyrd

Reputation: 76

If you are not going to deal with data on Server side and only on client you could just set the field to a string value. For the database field that stores this data be sure the is setup to account for potential size of all the JSON data and potentially character encoding as these could present issues.

 public string SomeNestedData { get; set; } 

If however you wanted to or could create a Model of the Data being used/stored for the JSON Data for (De)Serialization purposes you could to ensure only valid JSON data is stored in the field do something like the following...

private string JSONSomeNestedData  { get; set; }

public  {model}  SomeNestedData 
  { get { return Deserialize(JSONSomeNestedData); }}
  { set { JSONSomeNestedData = Serialize(value);  }}

Upvotes: 1

Related Questions