1

I'm using reflection to deserialize object from binary stream ("objType=SM.Backend.Entities.Erp.CodeListBase"):

var methodInfo = typeof(SM.Core.Serializator).GetMethod("DeserializeCache");
var genericMethod = methodInfo.MakeGenericMethod(objType);
var _objItem = genericMethod.Invoke(null, new[] { _streamedData });

It works. If I inspect _objItem in Locals window it looks like:_objItem

Now i would like to export _objItem content to json string:

var jsonString=JsonConvert.SerializeObject(_objItem, Formatting.Indented);

But result is empty: "[]". If I do the same for other objects it works. I guess for this object it doesn't work because it is complex type(nested):

SM.Backend.Entities.Erp.CodeListBase{SM.Backend.Entities.Erp.CodeList<SM.Backend.Entities.Erp.MaterialType>}.

Any idea how to extract the content from locals to json string? Should I convert object to different type first?

If I change _objItem to be iList of items of type:

SM.Backend.Entities.Erp.CodeList<SM.Backend.Entities.Erp.MaterialType>        
SM.Backend.Entities.Erp.CodeList<SM.Backend.Entities.Erp.ProductDescriptionType>

...

as you can see here: _objItem

the result is the same "[]". But collection is not empty.

Community
  • 1
  • 1
Simon
  • 1,955
  • 5
  • 35
  • 49
  • SerializeObject((SM.Backend.Entities.Erp.CodeList)_objItem, Formatting.Indented); could be some problem with type identification. – Mertuarez Dec 21 '16 at 10:57
  • By default, NewtonSoft.Json will only serialize public members http://stackoverflow.com/questions/29003215/newtonsoft-json-serialization-returns-empty-json-object – Mertuarez Dec 21 '16 at 11:01
  • Thanks. i think all members are public. And idea? – Simon Dec 21 '16 at 12:02
  • SM.Backend.Entities.Erp.CodeList _objItem = genericMethod.Invoke(null, new[] { _streamedData }); //try cast object to correct type or look at my first comment – Mertuarez Dec 21 '16 at 12:15
  • But something work fine because "[]" it mean that it detect IEnumerable otherway you will get "{}" – Mertuarez Dec 21 '16 at 12:28
  • Casting doesn't help.The result is the same - still []. – Simon Dec 21 '16 at 12:37
  • can you create that object without reflection and serialize that. Or are you able to add attribute [JsonProperty]? – Mertuarez Dec 21 '16 at 12:44
  • Your `CodeList` type must be some sort of collection, and Json.NET will serialize any non-dictionary collection as a JSON array, as explained [here](http://www.newtonsoft.com/json/help/html/serializationguide.htm#Lists). `"[]"` must be appearing because the collection is empty. – dbc Dec 21 '16 at 15:56
  • I have add also picture when _objItem is iList collection. And it is not empty, it is iList collection. – Simon Dec 22 '16 at 07:44

0 Answers0