16

I have the following dynamic object that I'm getting from a third party library:

IOrderStore os = ss.GetService<IOrderStore>();
IOrderInfo search = os.Orders.Where(t => t.Number == "test").FirstOrDefault();
IOrder orderFound = os.OpenOrder(search, true);

dynamic order = (dynamic)orderFound;
dynamic requirements = order.Title.Commitments[0].Requirements;

I need to parse it to a JSON string.

I tried this (using JSON.net):

string jsonString = JsonConvert.SerializeObject(requirements);
return jsonString;

But I get a seemingly corrupted JSON string, as below:

[{"$id":"1"},{"$id":"2"},{"$id":"3"},{"$id":"4"},{"$id":"5"},{"$id":"6"},{"$id":"7"},{"$id":"8"},{"$id":"9"},{"$id":"10"},{"$id":"11"},{"$id":"12"},{"$id":"13"},{"$id":"14"},{"$id":"15"}]

The object contains multiple properties, and not just the 'id'.

Any advice?

Sagar Zala
  • 4,854
  • 9
  • 34
  • 62
user3378165
  • 6,546
  • 17
  • 62
  • 101
  • show your object in which you want to convert – Hitesh Thakor Aug 08 '16 at 09:07
  • 'string json = Newtonsoft.Json.JsonConvert.SerializeObject(foo);' – Rashid Javed Aug 08 '16 at 09:10
  • @RashidJaved this is eactly what I am doing... – user3378165 Aug 08 '16 at 09:20
  • @HiteshThakor do you want me to show the object I want to assign the json? – user3378165 Aug 08 '16 at 09:21
  • 1
    @user3378165 - yes please show the way `Requirements` is created because when just simply testing a list of dynamic objects and each with several fields they are all serialized – Gilad Green Aug 08 '16 at 09:23
  • Have you tried creating a `JObject` from your `orderFound` object, and then serializing that instead? E.g. `dynamic order = new JObject(orderFound); var jsonString = JsonConvert.SerializeObject(order);` – Geoff James Aug 08 '16 at 09:30
  • @GiladGreen I can't show you how the `Requirements` object is created becasue I don't have that, I'm getting it from a third party library. – user3378165 Aug 08 '16 at 09:32
  • @user3378165 What type is your `orderFound` object, and why are you casting it to a `dynamic`? Could you show us that object? – Geoff James Aug 08 '16 at 09:33
  • So try and show it from debug.. or somehow.. because `JsonConvert` does work on a list of dynamic objects with several properties.. – Gilad Green Aug 08 '16 at 09:34
  • 1
    What you are trying to do looks perfectly correct. See [this working example](https://dotnetfiddle.net/w3gjn4). We _need_ to see the decalaration/definition of the `Requirements` property in order to help you. – Good Night Nerd Pride Aug 08 '16 at 09:47
  • @user3378165 - So, I might be missing the point; but have you tried just `SerializeObject()` on your `orderFound` object, rather than the dynamic one? As already stated - `dynamic` objects *should* still serialize just fine. My only thought in this case, is that the casting to `dynamic` is just flattening the `orderFound` somehow and it's getting serialized incorrectly. Just try serializing the `orderFound` to JSON string and let us know the result? – Geoff James Aug 08 '16 at 10:05
  • 1
    @user3378165, sorry, but your edits don't help. What does `Console.WriteLine(order.Title.Commitments[0].Requirements.GetType());` print? Show us that string, please. Also, you keep saying you are using a third party library. Which library? Is there documentation available? And most importantly: how you you know the returned JSON is "_corrupted_", as you say? What is your reference? What _exactly_ do you expect to see? – Good Night Nerd Pride Aug 08 '16 at 10:22
  • @Abbondanza It gives me an internal object of the third party library, it's a third party that we are working with without any relation to programming and in term of security I can't mention. There is a documentation, and they told me to use `dynamic`. When I tried to get rid of the `dynamic` I got a bounch of errors from their API. If I do use `dynamic` I can explicity access the properties but when I try to serialize it to JSON I get only the 'id' property, so maybe 'corrupted' is not the correct word... Thank you very much for your help. – user3378165 Aug 08 '16 at 10:49
  • 1) Json.NET supports dynamic object that implement `IDynamicMetaObjectProvider`. See [Serialization Guide: Dynamic](http://www.newtonsoft.com/json/help/html/serializationguide.htm#Dynamic). Perhaps the 3rd party library did something wrong there? 2) Perhaps there is something wrong with your [`DefaultSettings`](http://www.newtonsoft.com/json/help/html/p_newtonsoft_json_jsonconvert_defaultsettings.htm). Try setting them to null before serializing. – dbc Aug 08 '16 at 15:04

2 Answers2

14

Have you tried using var instead of dynamic?

// Use "var" in the declaration below.
var requirements = order.Title.Commitments[0].Requirements;
string jsonString = JsonConvert.SerializeObject(requirements);

When you only want to deserialize requirements without doing anything else with it then there is no need to use it dynamically.

Good Night Nerd Pride
  • 8,245
  • 4
  • 49
  • 65
6

Try using Convert.ToString() as following code to convert 'dynamic' object to 'string' -

dynamic order = (dynamic)orderFound;
dynamic requirements = order.Title.Commitments[0].Requirements;
string validString = Convert.ToString(requirements);
Nishant Singh
  • 4,177
  • 1
  • 17
  • 17
Sayit
  • 77
  • 1
  • 2