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
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 ...
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 ...
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 ...
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 ...
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 ...
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
{
...
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....
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....
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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....
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,
...
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) ...
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....
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;
...
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;
...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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" : "...
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. ...
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)]
...
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.
...
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 ...
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 ...
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(...
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 ...
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 ...
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 {...
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?
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 '...
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 ...
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, ...
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",...