5

Assume that I'm receiving endless stream of "anonymous" objects as (JSON/XML/whatever) that are supposed to be of same class, however not all objects contains all fields/properties, ex:

{
 object1 : {field1 : 1, field2: 2},
 object2 : {field1 : 3, field4: 5}
}

As you can see if I use either of objects as template and create a class matching it, the other one won't fit as there's a missing property, in the case of limited objects I can go over them and extract all fields then build a class that fits all and default the missing properties for each object.

However in an "endless" stream it's not possible to do it, so the only approach I found is creating Class1 that fits the first object and create the first object, when going to next object if there's an extra property I add it to Class1 and create the second object of Class1, then go again for each object. When Editing Class1 all objects that were made before the edit must include the new property without having to recreate them.

Any idea how to do this??

Note: I don't want to create a new class every time I find a new field, it'll take much time to recreate all old objects not to mention that number of objects is ever increasing.

10
  • 11
    Why do you need concrete classes at all, wouldn't something more dynamic fit your needs better, like a dictionary or expandoobject or something? Commented Aug 7, 2015 at 8:52
  • 4
    Don't declare any class. Use JObject.Parse("...") (in Json.Net). Now you have your object with all properties in it. Commented Aug 7, 2015 at 8:52
  • What if you use Dictionary<string, object> allProperties to store your properties like allProperties.Add("field1", 1); allProperties.Add("field4", 5); ? Commented Aug 7, 2015 at 8:56
  • @Eser I'm doing "manual" parsing. @Lasse V. Karlsen expandoobject gives error when trying to access a "property" that doesn't exist while I want to make it return default value for it depending on the type of property, I thought of something like a dictionary but I want to be able to access it like normal property/field with object.property Commented Aug 7, 2015 at 8:59
  • 1
    Try to improve your title man.... something like "deserialise heterogeneous array of objects" would do the trick... you don't wanna go for a big headline for something so meaning less, that could easily be solved with JSON NET / Expando Object / Dynamic Proxy... and by the way... if the expando object gives you error when trying to access a non existing property, it doesn't mean that you can't override the getter method so it won't throw an exception... Commented Aug 7, 2015 at 9:16

1 Answer 1

1

Use Json.Net library's JObect class.

1- You can use it with dynamic

string json1 = @"{object1 : {field1 : 1, field2: 2}}";
string json2 = @"{object2 : {field3 : 3, field2: 4}}";

dynamic obj = JObject.Parse(json2);
if (obj.object1 != null) Console.WriteLine(obj.object1.field1);
if (obj.object2 != null) Console.WriteLine(obj.object2.field3);

2- You can use it as dictionary

var jObj = JObject.Parse(json1);
if (jObj["object1"] != null) Console.WriteLine(jObj["object1"]["field1"]);

3- It supports Linq. You can enumerate it easily to get all children/descendants etc.

var rootProperties = jObj.Children().OfType<JProperty>()
                    .Select(p => p.Name).ToArray();
Sign up to request clarification or add additional context in comments.

2 Comments

I'm doing "manual" parsing, aka: I'm not using JSON.NET thus I don't have access to JObject.
@T.Aoukar: Can't you start using JSON.NET?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.