Questions tagged [json.net]

Json.NET (also known as Newtonsoft.Json) is a popular high-performance JSON framework for .NET.

json.net
3,121 questions with no upvoted or accepted answers
Filter by
Sorted by
Tagged with
13 votes
1 answer
2k views

AddNewtonsoftJson is not overriding System.Text.Json

I have upgraded my version of .Net Core from preview 2 to preview 6 which has broken a couple of things. Most significant is that I cannot use newtonsoft JSON anymore. AddNewtonsoftJson in ...
craig's user avatar
  • 441
13 votes
1 answer
6k views

An exception in Json.NET: Token PropertyName in state "Start" would result in an invalid JavaScript object

I see the error in my prod server log; can u give me any hint about what kinds of situations would trigger the error? Thanks. Token PropertyName in state Start would result in an invalid JavaScript ...
Ricky's user avatar
  • 35.1k
12 votes
3 answers
10k views

How do you implement System.Text.Json.Serialization.ReferenceHandler.Preserve in a Blazor WebAssemby application

It's my understanding that you can use this setting to get around the issue of getting the following error when you have circular references defined in your object model: JsonException: A possible ...
scooter's user avatar
  • 403
11 votes
3 answers
3k views

Could not load file or assembly 'Newtonsoft.Json, Version=6.0.0.0 ..." during runtime ,indirect dependency assembly keep looking for old version

I have a C# project that has direct reference to Newtonsoft.Json and some other references(which has indirect reference to Newtonsoft.Json). I want to upgrade Newtonsoft.Json version from 6.0.8 to the ...
Jeff Wang's user avatar
  • 111
11 votes
0 answers
4k views

Json.NET async support with custom JsonConverter

Json.NET version 10 added support to async serialization. Version 11 added support to generic JsonConverter<T>. The problem is that JsonConverter<T> and previous JsonConverter only ...
Bruno Garcia's user avatar
  • 6,204
11 votes
1 answer
2k views

ReferenceLoopHandling.Ignore not working in WebApi Global.asax

I have an API end point which is returning a loop error (as it links a joining class which loops back) so e.g. class A { virtual ClassAB; } class B { virtual ClassAB; } class AB { ...
Matthew Flynn's user avatar
10 votes
0 answers
580 views

newtonsoft json.net disable scientific notation

I would like my webapi to serialize json using camelcase. So i added the following to Global.asax. HttpConfiguration config = GlobalConfiguration.Configuration; config.Formatters....
M F's user avatar
  • 161
9 votes
0 answers
3k views

Swagger, NswagStudio, c#, Required = Newtonsoft.Json.Required.DisallowNull, how to handle

Given: api that i have limited influence to in terms of changing, built on net core 2.2. Standart netCore swagger used. Some classes of DTO have fields in it marked with [System.ComponentModel....
Andrey Stepanov's user avatar
9 votes
1 answer
2k views

Detected package version outside of dependency constraint

Detected package version outside of dependency constraint: Microsoft.NET.Sdk.Functions 1.0.13 requires Newtonsoft.Json (= 9.0.1) but version Newtonsoft.Json 10.0.1 was resolved. I am in need of ...
Maseeha Razzack's user avatar
9 votes
2 answers
7k views

Handling newlines in data with Json.Net

Is there any way to handle newlines in JSON.NET. I have some data coming back with Carriage Return Line Feed in it and Json.Net is just leaving it raw in the return value. Is there a way to force ...
Ronnie's user avatar
  • 670
8 votes
0 answers
1k views

Storing JContainers (JObject, JArray, etc) in mongodb with C# driver

I have a WEB API web service that accepts an arbitrary Object that I would like to store as a property in a MongoDB document. By the time my class library gets it, it is a JObject, so simply storing ...
Sally Richter's user avatar
8 votes
0 answers
990 views

Different NullValueHandling for serialization and deserialization in asp.net core web application

I'm trying to implement PATCH method using view models which implements INotifyPropertyChanged. That is why I want NullValueHandling to be NullValueHandling.Include for deserialization (so explicitly ...
THTP's user avatar
  • 1,376
8 votes
0 answers
2k views

Howto handle reference in CustomCreationConverter (json.net)

There are two classes public class Playlist { public string Name { get; set; } public Schedule Schedule1 { get; set; } public Schedule Schedule2 { get; set; } } public interface Schedule ...
nonocast's user avatar
  • 527
7 votes
0 answers
1k views

Disable System.Text.Json on Web API on .NET Core 3.0

I would like explicitly use legacy Newtonsoft.Json library for serialization/deserialization on .NET Core Web API 3.0.0. No mater what I do, always System.Text.Json is involved and custom Newtonsoft's ...
Rok's user avatar
  • 581
7 votes
1 answer
488 views

Newtonsoft.Json.JsonConvert.SerializeObject of BsonDocument containing null value fails with an InvalidCastException

The following test snippet demonstrates an InvalidCastException occurring in SerializeObject when an object value is null or even BsonNull.Value. If the value is changed to 42, serialization works ...
koan911's user avatar
  • 370
7 votes
1 answer
1k views

Can't compile project when replacing a reference to Json.NET with its source code

I have an ASP.NET Core 1.0 class library that I recently upgraded successfully to .NET Core 1.0 RTM. I use it to implement some custom Json input and output formatters to include in the ASP.NET Core ...
Chris's user avatar
  • 542
7 votes
0 answers
2k views

JsonSerializationException When Creating JToken from Mock Object

I have a following line of code that I am trying to get past in unit testing with mocking up a certain class that makes calls internally to databases (not by my design): var configuration = config....
cdalto's user avatar
  • 797
7 votes
0 answers
3k views

Json.NET does not use JsonProperty on deserialize

I'm trying to deserialize the following json { "category": "mycategory", "updated_date": "2011-11-10T03:07:43", "description":"description", "image": "someImageUrl", "longitude": 100, ...
vkudriashov's user avatar
7 votes
1 answer
4k views

JSON.NET CustomCreationConverter with nested objects

that is my very first question I ask on this site, so forgive me if I missed something. I have some problems deserializing an complex object graph using JSON.NET. My class hierarchy is (simplified) ...
CGR's user avatar
  • 101
6 votes
0 answers
1k views

MongoDB Issues Deserializing JArray

I'm getting following exceptions: An error occurred while deserializing the Value property of class ExampleClass: Type 'Newtonsoft.Json.Linq.JToken' does not have a suitable constructor or Add method....
fuji's user avatar
  • 331
6 votes
0 answers
1k views

What is the difference (if any) between the [JsonRequired] and [JsonProperty(Required = Required.Always)] attributes in Json.Net?

As mentioned in the title, although I've looked at the documentation for the following attributes, I don't understand what the different use cases would be. [JsonRequired] public string someProperty; ...
TritonJak's user avatar
6 votes
1 answer
1k views

Is there a way to get JSON.Net to serialize an object using implicit conversion instead of its actual type?

Consider this interface and classes... public interface IValueHolder{} public class ValueHolder<TValue> : IValueHolder { public ValueHolder(TValue value) => this.value = value; ...
Mark A. Donohoe's user avatar
6 votes
0 answers
7k views

Use JsonIgnore on certain inherited properties

I have a base class like this-ish: public class Baseclass { public string Id { get; set; } public string Type { get; set; } public string Name { get; set; } } ...and many classes that ...
robertpaulsen's user avatar
6 votes
1 answer
10k views

JsonConvert.SerializeObject adds \r\n

so currently I am working on a WebAPI and I run in the following error. While I try to return a List serialized with JsonConvert.SerializeObject(object) my second attribute (string JSON) get covered ...
user1021605's user avatar
6 votes
2 answers
865 views

Linq object properties disappear after serialization

I'm trying to understand the included code snippets behavior, it doesn't work as I'd expect. Here is what I'm doing: Serialize a LINQ object to JSON Deserialize the JSON at (1) back in its initial ...
guiomie's user avatar
  • 5,058
6 votes
2 answers
2k views

T4 templates only run when I "Debug" them

I'm having a bit of trouble here - my T4 templates that should generate C# classes refuse to successfully run when I save or "Run Custom Tool". However - it's only when I "Debug T4 Template" that it ...
NeroS's user avatar
  • 1,179
5 votes
0 answers
6k views

Serialize XmlDocument to Json using System.Text.Json in .Net Core 3.1 Web Api project

I'm switching from Newtonsoft.Json to System.Text.Json in a .net Core 3.1 web api project. The project is a legacy .net core web api project with hundreds of clients. Some of the controller endpoints ...
Sanny Jacobsson's user avatar
5 votes
0 answers
780 views

Deserialize json to a dictionary of different assembly and namespace

I am have issue mapping a json coming from a different project api to my model Customer for the property public Dictionary<string, List<CustomerAddress>> CustomerAddress{ get; set; } I ...
Ravio's user avatar
  • 125
5 votes
0 answers
2k views

Validate JsonSchema itself. Is it correct JSON Schema?

NB! I don't want to validate JSON against JSON Schema! I have 2 JSON files: 1. JSON Schema file: { "title" : "SomeTitle", "type":"object", "properties":{ "City" : {"type" : "...
kgzdev's user avatar
  • 2,810
5 votes
0 answers
268 views

Interface properties not getting serialized

If I defined a record that implements an interface with an additional property the interface properties are not serialized by json.net. How do I get json.net to serialize those interface properties. ...
jbeeko's user avatar
  • 457
5 votes
0 answers
1k views

Json.NET override property-level NullValueHandling with serializer

We have used property-level NullValueHandling to ignore null-values when serializing object: [Newtonsoft.Json.JsonProperty("Topic", NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)] ...
Mikael Koskinen's user avatar
5 votes
1 answer
4k views

Cannot Deserialize ClaimsPrincipal

Given I create a claims identity and subsequently a principal. Then I serialize the principal. Inspecting the json string I can confirm that the "Role" claim is there as well as the identity. ...
Cristian E.'s user avatar
  • 3,355
5 votes
1 answer
1k views

Is there a JSON equivalent of xsd.exe?

I am able to generate an XSDs for MyDataContract.dll assembly using xsd.exe xsd.exe MyDataContract.dll This will generate an schema0.xsd with definitions for all the types in the assembly. Is there ...
Darragh's user avatar
  • 2,574
5 votes
0 answers
3k views

Putting a "z" at the end if it is "datetime" and not "date" (JsonSerializerSettings)

Facts: In SQL Server there is the column type datetime and date Using EF, both datetime and date are mapped to C#'s DateTime In my datetime columns I'm always storing the UTC date. In my current ...
sports's user avatar
  • 7,999
5 votes
1 answer
2k views

JsonConvert.PopulateObject not using JsonConverter class attribute

When trying to update the class properties using JsonConvert.PopulateObject the JsonPathConverter is not called and therefore populate is not done. Sample class: [JsonConverter(typeof(...
VeYroN's user avatar
  • 760
5 votes
0 answers
509 views

FromUri in WebAPI action doesn't use JSON.NET JsonParameter to fetch parameters value

I'm working on a GET asp.net WebAPI action to accept a complex structure (GET is the requirement): public class Input { [JsonProperty("a")] public string PropA {get; set;} ... } and my ...
Mando's user avatar
  • 11.6k
5 votes
0 answers
276 views

JSon serialization with azure mobile service

I am using azure mobile service (backend sdk 1.x) and I serialize table objects to Json. config.Formatters.JsonFormatter.SerializerSettings.TypeNameHandling = TypeNameHandling.Objects; I would like ...
BanditoBunny's user avatar
  • 3,766
5 votes
0 answers
776 views

Alternative solution for JSON.NET attributes

Currently in my model classes I have some JSON.NET attributes like public class MyModel { [JsonProperty("_anothername")] [JsonConverter(typeof(MyCustomConverter))] public string Name {...
Cheng Chen's user avatar
  • 43.1k
5 votes
1 answer
3k views

Are there any known backward compatibility issues with upgrading Newtonsoft.JSON?

Are there any known backward compatibility issues with upgrading Newtonsoft.JSON, specifically in my case from 4.5.9 to 6.0.8?
Sadek Noureddine's user avatar
5 votes
0 answers
7k views

Error de-serializing the object of type DateTime - does not start with '\/Date(' and end with ')\/' as required for JSON

I have stable web-api solution and I am accessing api methods from MVC controller using HttpWebRequest. GET and POST working fine without Date Time object.But when data returned from web api has Date ...
iShareHappyCode's user avatar
5 votes
1 answer
1k views

Custom JSON to XML conversion using JSON.Net

I am using (Newtonsoft's) JSON.Net library with C#, and I want to customize a single part of its default XML conversion. For example, if I wanted to change the XML conversion of arrays, than say I ...
EZLearner's user avatar
  • 1,714
5 votes
1 answer
4k views

MissingMethodException from Async serialisation operation

I have written a small Async library wrapper to Json.Net v5.06 from NuGet and after updating a number of Telerik packages my UnitTests started to fail with a MissingMethodException. I have created a ...
Phil Murray's user avatar
  • 6,456
5 votes
1 answer
1k views

Significant bug in Json.NET dictionary serialization

Json.NET does not always properly take into account the declared ValueType in a Dictionary<KeyType,ValueType>. This makes serializing a Dictionary<string,object> pretty unworkable if a ...
aggieNick02's user avatar
  • 2,697
5 votes
1 answer
805 views

Strange issue when adding a service reference to a project that has the JSON.NET library referenced

The project is your standard run-of-the-mill ASP.NET MVC 3 application that communicates with an in house WCF service. Our ASP.NET MVC application references a few external libraries, including the ...
Zach Day's user avatar
5 votes
2 answers
1k views

Json and parsing C#

I need your help. I am new to C#, so I would appreciate any help! So I made a program which parses a json file and puts it into an object. My program looks like this: var path = (@"C:\sim\input7....
5 votes
1 answer
15k views

Input string in not a valid number - JSON deserialization

I have this array: { "AssemblyVersion":"0.1.333.5973", "Exception": { // [...] } } which is serialized from this class: public class ErrorData { public string ...
Christian Klemm's user avatar
4 votes
0 answers
2k views

Deserialized JSON type 'Newtonsoft.Json.Linq.JArray' is not compatible with expected type 'Newtonsoft.Json.Linq.JObject'

When I run "Populate" after update the version of Newtonsoft.Json to 13.0.1 I get an error: 'Deserialized JSON type 'Newtonsoft.Json.Linq.JArray' is not compatible with expected type '...
Roy Yadid's user avatar
4 votes
0 answers
4k views

System.Text.Json Error Handling equivalency to Newtonsoft.Json (json.net) JsonSerializerSettings

We are following in .Net Core's path in transitioning from Newtonsoft.Json to the new System.Text.Json. One feature of Newtonsoft that we found convenient was to provide an error handler in the ...
denver's user avatar
  • 2,965
4 votes
0 answers
2k views

DefaultContractResolver equivalent in System.Text.Json

I am converting some logic from Newtonsoft and found one of following implementation public class CustomDataContractResolver : DefaultContractResolver { public Dictionary<string, ...
Kamran Shahid's user avatar
4 votes
3 answers
2k views

Deserialize an array inside a json object using JsonConvert.DeserializeObject()

I have a requirement where I have incoming json object of the following format: { "CustomerList": { "Customer": [{ "CustomerCode" : "C123",...
user7461027's user avatar

1
2 3 4 5
63