Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serializing single instance to multiple JSON strings

Tags:

c#

json.net

Given the following class:

public class Config {
    public string Property1 {get; set;} = "foo";
    public string Property2 {get; set;} = "bar";
    public string Property3 {get; set;} = "baz";
    public string Property4 {get; set;} = "baz1";
}

I want to serialize an instance of this class into two separate JSON strings. Some of the properties should go into the first JSON string:

{
    "Property1": "foo",
    "Property2": "bar"
}

while the rest of the properties should go into the other JSON string:

{
    "Property3": "baz",
    "Property4": "baz1"
}

The division of properties is always the same, and properties will go either to one or the other.

(I can deserialize the two JSON strings back into a single object using JObject.Merge.)

Currently, I am writing to a pair of JObject instances, but that is a maintenance nightmare (json["Property1"] = x.Property1; json["Property2"] = x.Property2; etc.).

How can I do this in a way which is easier to maintain?

like image 336
Zev Spitz Avatar asked Dec 06 '25 05:12

Zev Spitz


2 Answers

Making life complicated for no reason but whatever. Here is one way to solve the problem:

  1. Create your own attribute class. Can be either one class per serialization "batch" or one class with the name of the serialization. For example, the name of the attribute class could be JsonSerializationBatchAttribute

  2. Decorate your data members with that attribute like this:

    public class Config {
        [JsonSerializationBatch("1")]
        public string Property1 {get; set;} = "foo";
        [JsonSerializationBatch("1")]
        public string Property2 {get; set;} = "bar";
        [JsonSerializationBatch("2")]
        public string Property3 {get; set;} = "baz";
        [JsonSerializationBatch("2")]
        public string Property4 {get; set;} = "baz1";
    }
  1. Then implement conditional serialization as described here:

https://www.newtonsoft.com/json/help/html/ConditionalProperties.htm

in the serialization function check if a property has JsonSerializationBatch attribute with proper string and ignore all properties that do not.

I would do this complex thing only if I would have many objects that need this type of serialization. If only one object requires such serialization then I would lean towards splitting class into more than one or using anonymous objects for serialization.

like image 172
Optional Option Avatar answered Dec 09 '25 20:12

Optional Option


You could try using anonymous objects, like so:

var string1 = JsonConvert.SerializeObject(new {property1: config.Property1, property2: config.Property2});
var string2 = JsonConvert.SerializeObject(new {property3: config.Property3, property4: config.Property4});

PS: I'm assuming you are using newtonsoft.json if not, just replace the serialize method.

Hope to have helped!

like image 41
Vitor Ribeiro Avatar answered Dec 09 '25 19:12

Vitor Ribeiro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!