json.net

MissingMethodException with Newtonsoft.Json when using TypeNameAssemblyFormat with PCLs

霸气de小男生 提交于 2019-12-09 02:53:10
问题 Is there a problem with using the TypeNameAssemblyFormat with PCLs? I have no problems using any other settings with Newtonsoft.Json except when I use this serialization setting. Here is my Json-related code: var settings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Objects, Formatting = Formatting.Indented, TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Full }; var json = JsonConvert.SerializeObject(obj, settings); var

How to preserve timezone when deserializing DateTime using JSON.NET? [duplicate]

心已入冬 提交于 2019-12-09 02:41:58
问题 This question already has answers here : Parse DateTime with time zone of form PST/CEST/UTC/etc (6 answers) Closed 5 years ago . I'm parsing some JSON in C# using JSON.NET. One of the fields in the JSON is a date/time, like this: { "theTime":"2014-11-20T07:15:11-0500", // ... a lot more fields ... } Note that the time part is 07:15:11 (TZ of GMT-5 hrs) I parse the JSON from a stream like this: using (var streamReader = new StreamReader(rcvdStream)) { JsonTextReader reader = new JsonTextReader

How to omit empty collections when serializing with Json.NET

帅比萌擦擦* 提交于 2019-12-09 02:29:42
问题 I'm using Newtonsoft's Json.NET 7.0.0.0 to serialize classes to JSON from C#: class Foo { public string X; public List<string> Y = new List<string>(); } var json = JsonConvert.SerializeObject( new Foo(), Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); The value of json here is { "Y": [] } but I would like it to be { } if Y is an empty list. I couldn't find a satisfactory way to achieve this. Maybe with a custom contract resolver? 回答1: If you

How do I deserialize a JSON array and ignore the root node?

╄→尐↘猪︶ㄣ 提交于 2019-12-09 02:12:37
问题 I have next response from server - {"response":[{"uid":174952xxxx,"first_name":"xxxx","last_name":"xxx"}]} I am trying to deserialize this in next way - JsonConvert.DeserializeObject<T>(json); Where T = List of VkUser, but I got error. [JsonObject] public class VkUser { [JsonProperty("uid")] public string UserId { get; set; } [JsonProperty("first_name")] public string FirstName { get; set; } [JsonProperty("last_name")] public string LastName { get; set; } } I always tryed public class SomeDto

Json convert object which inherit dictionary

不羁岁月 提交于 2019-12-09 01:33:18
问题 I have following class definition: public class ElasticObject : Dictionary<string, object> { public int Id { get;set;} } var keyValues = new ElasticObject(); keyValues.Id= 200000; keyValues.Add("Price", 12.5); var json = JsonConvert.SerializeObject(keyValues, new JsonSerializerSettings{NullValueHandling = NullValueHandling.Ignore}); The parsed json string is {"Price":12.5} which fails to contain the Id property, is there any way to customize the json conversion? 回答1: You can do this by making

Deserialize multiple json objects from a stream using Newtonsoft Json

雨燕双飞 提交于 2019-12-08 20:21:09
问题 I am reading a NetworkStream for json string and then deserializing it using Newtonsoft.Json . Sometimes, two json objects could be sent back to back and read at the same time on the stream. But the Newtonsoft.Json serializer gives me only one object. For example, if I have the following string on the stream: {"name":"John Doe","age":10}{"name":"Jane Doe","age":10} If I deserialize the stream, the serializer reads the entire stream, but gives only the first object. Is there a way to make the

JSON.NET Deserialization in C# results in empty object

不羁岁月 提交于 2019-12-08 19:46:09
问题 I am trying to populate a C# object (ImportedProductCodesContainer) with data using JSON.NET deserialization. ImportedProductCodesContainer.cs: using Newtonsoft.Json; [JsonObject(MemberSerialization.OptOut)] public class ImportedProductCodesContainer { public ImportedProductCodesContainer() { } [JsonProperty] public ActionType Action { get; set; } [JsonProperty] public string ProductListRaw { get; set; } public enum ActionType {Append=1, Replace}; } JSON string: {

How to serialize a JObject the same way as an object with Json.NET?

馋奶兔 提交于 2019-12-08 19:14:29
问题 How do I control the serialization of a JObject to string? I have some APIs that return a JObject and I usually apply some changes and persist or return them. I want to avoid persisting null properties and apply some additional formatting, but JsonConvert seems to completely ignore my settings. Here is the sample of the problem: // startup.cs has the following services.AddMvc().AddJsonOptions(o => { o.SerializerSettings.NullValueHandling = NullValueHandling.Ignore; }); public class

Newtonsoft.Json adds ellipsis (…) at the middle of a serialized array

↘锁芯ラ 提交于 2019-12-08 17:32:07
问题 This is a very weird behaviour of Newtonsoft.Json 's serialization functionality. I've tried almost everything (for instance I didn't go ahead and use .NET Reflector to walk step by step through the Newtonsoft.Json.dll assembly's algorithms). Symptoms The situation is as follows: I have a small POCO which holds 4 string properties: public class MyPoco { public string op { get; set; } public string left { get; set; } public string right { get; set; } public string result { get; set; } } I

“Value cannot be null” when passing null arguments to SignalR client-side proxy

天涯浪子 提交于 2019-12-08 17:05:48
问题 Using SignalR (v2.0.2) I'm getting this exception: Value cannot be null. Parameter name: o at Newtonsoft.Json.Utilities.ValidationUtils.ArgumentNotNull(Object value, String parameterName) at Newtonsoft.Json.Linq.JToken.FromObjectInternal(Object o, JsonSerializer jsonSerializer) at Newtonsoft.Json.Linq.JToken.FromObject(Object o, JsonSerializer jsonSerializer) at Microsoft.AspNet.SignalR.Client.Hubs.HubProxy.Invoke[T](String method, Object[] args) I'm calling the HubProxy with reflection, and