json.net

Is it possible to serialize DateTimeOffset to zulu time string with Json.NET?

隐身守侯 提交于 2019-12-22 04:06:33
问题 I have a DateTimeOffset object of "05/06/2014 05:54:00 PM -04:00". When serializing using Json.NET and ISO setting, I get "2014-05-06T17:54:00-04:00". What I would like to have is the UTC/Zulu version of that string "2014-05-06T21:54:00Z". However, I could not find any serializer setting to achieve this. I know for DateTime serialization, I can set DateTimeZoneHandling = DateTimeZoneHandling.Utc to have the Zulu format. However, there isn't such setting option for DateTimeOffset. Am I missing

Can I distribute JSON.NET with my commercial application? [closed]

可紊 提交于 2019-12-22 03:59:33
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . I am looking for a .NET JSON library that I can use to access JSON objects in a xpath like fashion. For example, given a JSON object in a string, can access its attributes by specifying a path in another string. Well, I found JSON.NET will do the job, however I am not sure if I can use and distribute it with my

JSON.NET Serialize DateTime.MinValue as null

让人想犯罪 __ 提交于 2019-12-22 03:44:56
问题 I'd like DateTime fields that are set to DateTime.MinValue returned by my Web API to be serialized to NULL instead of "0001-01-01T00:00:00" . I understand there's a way to get JSON.NET to omit fields that are set to default values, but I would prefer JSON.NET to specifically serialize DateTime MinValue / "0001-01-01T00:00:00" as null . Is there a way to do this? 回答1: Create a custom converter which serializes DateTime.MinValue into null , and (if required) deserializes null into DateTime

Json.net deserializing list gives duplicate items

自古美人都是妖i 提交于 2019-12-22 02:48:22
问题 I have just started using Newtonsoft.Json (Json.net). In my first simple test, I ran into a problem when deserializing generic lists. In my code sample below I serialize an object, containing three types of simple integer lists (property, member var and array). The resulting json looks fine (the lists are converted into json-arrays). However, when I deserialize the json back to a new object of the same type, all list items are duplicated, expect for the array. I've illustrated that by

Json.net fails when trying to deserialize a class that inherits from Exception

Deadly 提交于 2019-12-22 01:43:48
问题 I have a class SearchError that inherits from Exception , and when ever I try to deserialize it from a valid json I get the following exception: ISerializable type 'SearchError' does not have a valid constructor. To correctly implement ISerializable a constructor that takes SerializationInfo and StreamingContext parameters should be present. Path '', line 1, position 81. I tried implementing the suggested missing constructor, and it didn't help. This is the class after implementing the

Newtonsoft JsonConvert.SerializeObject ignoring JsonProperty if name is uppercase

时光毁灭记忆、已成空白 提交于 2019-12-22 01:29:59
问题 I want to be able to use the CamelCasePropertyNameContractResolver but override it for specific property names. For this, I use the JsonProperty attribute. This works fine except when the name that I choose is fully uppercase. Any ideas what's wrong or how to get around it? In the example below, Bar is serialized to "BAR" when I don't use the CamelCasePropertyNameContractResolver, but is serialized to "bar" when I do use the resolver. Foo and CamelCaseProperty are serialized correctly in both

JsonConverter equivalent for HTTP GET parameter

↘锁芯ラ 提交于 2019-12-21 20:47:47
问题 When writing C# web API controllers for HTTP POST functions, I can use attributes from Newtonsoft JSON on the properties of the parameter object. In particular, I can use a JsonConverter attribute on properties of an enum type to convert a string representation received from the client into one of the enum values (and back, for response objects): public class MyArgumentsDTO { [JsonConverter(typeof(SomeEnumConverter))] public SomeEnum MyValue { get; set; } } // in the controller: [Route(

JsonConverter equivalent for HTTP GET parameter

牧云@^-^@ 提交于 2019-12-21 20:47:11
问题 When writing C# web API controllers for HTTP POST functions, I can use attributes from Newtonsoft JSON on the properties of the parameter object. In particular, I can use a JsonConverter attribute on properties of an enum type to convert a string representation received from the client into one of the enum values (and back, for response objects): public class MyArgumentsDTO { [JsonConverter(typeof(SomeEnumConverter))] public SomeEnum MyValue { get; set; } } // in the controller: [Route(

Remove Null values in JSON and Update JSON

我们两清 提交于 2019-12-21 20:27:57
问题 I have JSON Array as a string by serializing a list using Newtonsoft as below [{"ID":"1","Name":"somename","Class":"12","Section":null},{"ID":null,"Name":"somename","Class":"13","Section":null},{"ID":2,"Name":"somename","Class":null,"Section":"A"}] I need to convert this JSON by removing the NULL values to another JSONString like below [{"ID":"1","Name":"somename","Class":"12",},{"Name":"somename","Class":"13",},{"ID":2,"Name":"somename","Section":"A"}] Is there a way I can use Newtonsoft for

Json.NET - controlling class object-properties deserialization

久未见 提交于 2019-12-21 19:55:16
问题 I have a model class Link which is deserialized with JSON.Net. public class Link { [JsonConstructor] internal Link(int id) { Id = id; } public int Id { get; internal set; } [JsonProperty("title")] public string Title { get; internal set; } [JsonProperty("description")] public string Description { get; internal set; } ... and so on public Avatar AuthorAvatar { get; internal set; } } Avatar contains three properties: DefaultImageUri , SmallImageUri , MediumImageUri . Is it possible to create