json.net

JSON.NET Serialization on an object with a member of type Stream?

帅比萌擦擦* 提交于 2019-12-06 18:41:42
问题 Hopefully this is an easy fix that I have overlooked. I have an object passed into an event handler that I want to serialize that object using JSON.NET, like so: public void OnEvent(IEventObject foo) { // Serialize foo to string/disk here? var data = JsonConvert.SerializeObject(foo, Formatting.Indented); } It appears that one or more of foo's members are streams. I already recognize that Streams are not serializable since they are an abstraction over data and not the data itself. This makes

C# Json Convert any dynamic object to key value pairs

丶灬走出姿态 提交于 2019-12-06 17:29:31
I am writing a tool that will take an inbound Json object, and convert it to key-value records (sometimes called flattening, maybe). The aim is to avoid the tool breaking if it gets a very large or very nested Json object, so I would like to avoid recursion. An example object might be like this (below), containing nested arrays, empty values, you name it, literally any legal json... { "firstName": "John", "lastName": "Smith", "isAlive": true, "age": 25, "address": { "streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": "10021-3100" }, "phoneNumbers": [ { "type":

Convert an array of string into JArray

巧了我就是萌 提交于 2019-12-06 17:28:49
问题 I have an array of string var ids = new string[] { "1408576188", "1750854738", "100001058197465" }; I want to pass this array of string as a json array into an API. For now, the API cannot accept the string returned from : JsonConvert.SerializeObject(ids); So I am figuring out that I am able to use the API by turning my ids array into a JArray object. JArray.Parse(JsonConvert.SerializeObject(ids)); As you can see, I am doing a two operation in here, first I serialize the ids array, then I

How to pass parameter to constructor deserializing json

≡放荡痞女 提交于 2019-12-06 17:25:28
问题 I have a small problem with passing some parent instance to a constructor when deserializing an object with Newtonsoft.Json . Let's assume i have the following classes public class A { public string Str1 { get; set; } public IList<B> Bs { get; set; } } public class B { public B(A a) { // a should not be null! Console.WriteLine(a.Str) } } And now i serailze and than deserialize the object a like this: A a = new A() a.Bs = new List<B>() a.Bs.Add(new B(a)); a.Bs.Add(new B(a)); a.Bs.Add(new B(a))

How can Json.NET perform dependency injection during deserialization?

£可爱£侵袭症+ 提交于 2019-12-06 17:13:58
问题 When I have a class with no default constructor, i.e. using dependency injection to pass its dependencies, can Newtonsoft.Json create such an object? For example: public class SomeFoo { private readonly IFooDependency _dependency; public SomeFoo(IFooDependency dependency){ if(dependency == null) throw new ArgumentNullException("dependency"); _dependency = dependency; } public string Data { get; set; } public int MoreData { get; set; } public void DoFoo(){ Data = _dependency.GetFooData();

JSON.NET Parses +00:00 timezone as local times, but Z as UTC

别说谁变了你拦得住时间么 提交于 2019-12-06 16:42:27
I've been having an issue with Web API parsing datetimes incorrectly which I have tracked down to JSON.NET. The issue is that if I send this datetime: 2015-07-28T19:06:01.000+00:00 in a JSON PUT request, the DateTime parsed in my model will be converted to a time in the local server timezone, with the C# datetime kind of local, not UTC. If I send this datetime: 2015-07-28T19:06:01.000Z It will correctly keep it as UTC, with the C# datetime kind of UTC which is what I want. I can fix this, by setting the DateTimeZoneHandling like this: SerializerSettings.DateTimeZoneHandling =

Custom Json Converter with dependecy

与世无争的帅哥 提交于 2019-12-06 16:38:02
I have to use a custom JsonConverter with ASP.NET Core for a reason, and I need to use it with JsonInputFormatter . The only way I've found is to use AddJsonOption extension method like this: services .AddMvc() .AddJsonOptions(jso => jso.SerializerSettings.Converters.Add(new CustomConverter())) But it has a flaw: CustomConverter requires a dependency from a DI container which cannot be easily solved at configuration time. So the question: is there any programmer friendly way to supply a JsonConverter with dependency to ASP.NET Core JsonInputFormatter ? One quick workaround would be to postpone

JSON deserialization of derived types

て烟熏妆下的殇ゞ 提交于 2019-12-06 16:21:23
class Attribute1 { } class Attribute2 : Attribute1 { } class class1 { Attribute1 attr1; } class class2 : class1 { Attribute2 attr2; } var serializerSettings = new JsonSerializerSettings(){TypeNameHandling = TypeNameHandling.Objects}; class2 a = SomeValidObjectoftype Class2; string serializedClass2 = JsonConvert.SerializeObject(a, serializerSettings); var am = JsonConvert.DeserializeObject<Class2>(serializedClass1); All the above are JSON properties and objects. What I am trying to do is serialize and deserialize and not lose the type. While deserializing I lose the type of am.attr2. Currently

Serialize hashtable using Json.Net

独自空忆成欢 提交于 2019-12-06 16:21:15
I have a hashtable whose keys are of type integer, however when deserializing using json.net the keys come back as strings, is there a way to keep the key type on hashtable using json.net serialization/deserialization? This hashtable is a property of the type 'MyType' var settings = new JsonSerializerSettings(); settings.TypeNameHandling = TypeNameHandling.Objects; string json = JsonConvert.SerializeObject(o, Formatting.Indented, settings); mo = JsonConvert.DeserializeObject<MyType>(json, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Objects }); public Hashtable jsonViews {

Formatting output of Newtonsoft.Json.JsonConvert.SerializeObject(dataSet) [duplicate]

为君一笑 提交于 2019-12-06 16:04:54
问题 This question already has answers here : How do I make my JSON less verbose? (5 answers) Closed 6 years ago . I have a DataTable. When it's serialized into JSON with Newtonsoft.Json.JsonConvert.SerializeObject(dataTable) I get results in the following format: [ { "CLICK_KEY": 6254523744, "WEB_SERVER_KEY": 291, "PREV_CLICK_KEY": 0, "NEXT_CLICK_KEY": 0, "SESSION_KEY": 214981151, *more key value pairs* }, { "CLICK_KEY": 6254523745, "WEB_SERVER_KEY": 291, "PREV_CLICK_KEY": 0, "NEXT_CLICK_KEY": 0,