Questions tagged [jsonconverter]

111 questions
3
votes
1 answer

How to serialize/deserialize json dictionary into array

Need to serialize and deserialize C# dictionaries into a JSON array. I would like to also read the JSON from powershell using array index notation. By default, JSON format is: { "defaultSettings": { "applications": { "Apollo": { …
Arjay H.
  • 179
  • 1
  • 2
  • 8
2
votes
1 answer

Newtonsoft custom converter, string array as parameter

I am trying to create custom json converter ,which can parse date using specified formats public class MultiFormatDateConverter: JsonConverter { public override bool CanWrite => true; public override bool CanConvert(Type objectType) { …
2
votes
2 answers

System.Text.Json JsonConverter String array to single string

I would like to convert the following JSON. As you can see, the array has a single entry. I would like to convert it into a simple string. { "@timestamp": [ "2022-05-24T01:53:32.600Z" ], "site.siteCode": [ "ZAR" ], …
araqiel
  • 49
  • 7
2
votes
1 answer

Resolve cycle references of complex type during JSON serialization using System.Text.Json.Serialization.JsonConverter

There is a complex type with a reference to the object of the same type (sometimes to the same object): public class User { public string Name { get; set; } public int Age { get; set; } public User Reference { get; set; } } There is a…
2
votes
2 answers

Which approach to use for model binding a custom type

My API controller has an endpoint that looks like this: public async Task Update([FromBody] UpdateCommand command) { /* ... */ } That command looks like this: public class UpdateCommand { public string Username { get; set; } …
lonix
  • 14,255
  • 23
  • 85
  • 176
2
votes
0 answers

ASP.NET Core 5 doesn't respect neither JsonConverter nor TypeConverter

The title is pretty self explanatory. ASP.NET Core 5 doesn't respect neither the JsonConverter nor the TypeConverter. The symbols e.g. "TRX/USDT" should be split by / and converted to new Symbol("TRX", "USDT") as I already did that. Basically, I…
nop
  • 4,711
  • 6
  • 32
  • 93
2
votes
2 answers

How to apply a custom JsonConverter to the values inside a list inside a dictionary?

I have a CustomConverter : JsonConverter for integers, and I need to add a [JsonConverter(typeof(CustomConverter))] attribute to a Dictionary> property. Applying the custom converter to an int, List or Dictionary works…
Lars
  • 23
  • 3
2
votes
0 answers

Text.Json custom JsonConverter ignored

I have two Json Converters, one converts dates as MM/dd/yyyy, and another as MM/dd/yy. Second converter (listed in the code example first) is always ignored, and the first one used. Is there a way to have multiple converters for DateTime, and use…
K.S.
  • 110
  • 9
2
votes
1 answer

In a custom JsonConverter, how can I determine whether a Utf8JsonReader numeric token is a decimal or a long?

I have this jsonconverter which needs to convert a given property value either to a decimal or a long, depending on the value - but I can't seem to determine when the propertyvalue is decimal or long, since the tokentype only can detect number...…
kafka
  • 573
  • 1
  • 11
  • 28
2
votes
1 answer

Implementation custom Newtonsoft JsonConverter (reader)

I have an object that is written to Json using a custom Newtonsoft Jsonconverter. The object has two variables (array of vec3 points and array of triangleindices). The values need to be stored as base64 strings inside the json. The writer works as…
Schiette
  • 25
  • 4
2
votes
1 answer

Access already deserialized properties from JsonConverter.ReadJson

I am trying to solve backward compartibility when deserializing old json. Previously there was double property and now it's changed to a custom type. My idea is to read double and simply convert it using custom json converter. Before was: public…
Sinatr
  • 20,892
  • 15
  • 90
  • 319
2
votes
0 answers

Overriding [JsonConverter(typeof(UsualConverter)] class attribute

Test on Fiddle: https://dotnetfiddle.net/RyxMjm using NewtonSoft.Json; Let SomeClass be a library class that needs a custom json converter. The library it comes in provides the custom json converter, UsualConverter, and links the converter to the…
bboyle1234
  • 4,859
  • 2
  • 24
  • 29
1
vote
1 answer

InvalidCastException in custom JsonConverter

I am attempting to create a null safe json converter that handles json string values like "null" and return a default instance of a type. So far it's been working as expected, but I'm running into a casting issue with nullable integers in a nested…
shoop
  • 285
  • 3
  • 11
1
vote
0 answers

Custom System.Text.Json Converter string not working

I wrote a string custom System.Text.Json converter public class TranslatableJsonConverter : JsonConverter { private readonly IHttpContextAccessor _httpContextAccessor; public TranslatableJsonConverter(IHttpContextAccessor…
ciolo
  • 117
  • 10
1
vote
1 answer

How do I run a converter within another converter to handle multi-format content in a complex JSON file in C#?

I am trying to deserialize a JSON file with a complex structure in C#, but I'm facing difficulties due to multi-format content within arrays. Unfortunately, I cannot change the JSON format. Here's an example of the JSON file: { "messages": [ …