13

Just trying to create an anonymous type from JSON without knowing anything about the parameters ahead of time, and fully interpreting them (possibly with hints). i.e. that value "looks" like an int, string, or date.

The only way that I know of so far is to create an anonymous type that you have pre-knowledge of. See the .DeserializeAnonymousType(...) method below.

Can anyone do better than this? Thanks.

        var jsonString = "{\"user_id\": 1, \"user_type\": \"moderator\", \"name\": \"Fred\"}";

        JToken root = JObject.Parse(jsonString);

        var anonTemplate = new{user_id=0, user_type="", name="" };

        var a = JsonConvert.DeserializeAnonymousType(root.ToString(), anonTemplate);


        var b = JsonConvert.DeserializeObject<Object>(root.ToString());  // actually turns into a JsonObject which is something differet.

Update

I downloaded dynamicduck and am playing with it a little. Will this weird dynamic "wrapper" idea of Brian's be -able in the ways I need (serializable, etc)?

http://weblogs.asp.net/britchie/archive/2010/08/05/json-net-dynamic-extensions.aspx

http://weblogs.asp.net/britchie/archive/2010/08/03/dynamicduck-duck-typing-in-a-dynamic-world.aspx

5
  • In case of .NET 4 I assume that you should be able deserialize to dynamic object and then explicitly access required properties Commented Nov 3, 2011 at 13:41
  • 2
    How do you intend to consume this object if you do not know what parameters it has or is expected to have? Commented Nov 3, 2011 at 13:50
  • @sll: got an example? Not against dynamic types. I'm using them on occasion. In that case, my next question would be how do you convert from dynamic to an anon type. I'm happy with either path because performance is not an issue, however, I do need an anon type in this case (existing libraries). Commented Nov 3, 2011 at 13:52
  • @KallDrexx: there's other dynamic DSL stuff going on. Commented Nov 3, 2011 at 13:53
  • <a href="drowningintechnicaldebt.com/ShawnWeisfeld/archive/2010/08/22/… article</a> outlines how to return a C# dynamic, although not with Json.net Commented Nov 3, 2011 at 13:56

1 Answer 1

5

You can deserialize with JSON.NET to an ExpandoObject using the ExpandoObjectConverter.

To create an anonymous type at runtime, its quite a bit more complicated, take a look at the hoops jumped through in this thread:

How to create LINQ Expression Tree to select an anonymous type

As you can see quite a bit of effort that probably is not worth it as you still wouldn't get any compile time safety. So a dynamic or ExpandoObject are your best bets.

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

1 Comment

do you have a example of ExpandoObjectConverter usage that you could link to?

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.