1

I deserialize a JSon string that holds an object with an array of subobjects.
The current working solution looks like this:

var definition = new { systems = new subclass[0] };
var ret = JsonConvert.DeserializeAnonymousType(source, definition);

public class subclass
{
    public long id;
}

Is there a way to replace the subclass with another anonymous one?
I tried using the following, but only get a compiler error:

var definition = new { constellations = new{ id=0L }[0] };
1
  • Use Dynamic Commented Nov 2, 2016 at 9:46

1 Answer 1

2

I wonder if you could do:

var definition = new { systems = MakeEmptyArrayFrom(new { id = 0L}) };
...
static T[] MakeEmptyArrayFrom<T>(T value) => new T[0];

note: if that works, it will probably also work even if you use something like:

static T[] MakeNullArrayFrom<T>(T value) => null;

as I imagine the library is more interested in the Type than the value.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, it works. Even the null version. As I have multiple of these it will help declutter the code

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.