serialization shows only value kind and NOT the value
Asked Answered
B

4

5

We have data coming from 3rd party system and I have one class designed like this,

 public class CollectionProperty
{
    public string Name { get; set; }
    public object Value { get; set; }
}

and my VS debugger saying value like this which gives me extra details of data type along with result,

enter image description here enter image description here

Now when I serialize this using Newtonsoft,

 var x = JsonConvert.SerializeObject(resultPacket);

it's giving below output with only value kind and NOT the value.

I need string value with double quote, number value withOUT double quote, how to do this?

[{"Name":"Device ID","Value":{"ValueKind":3}}]},{"Name":"CPU0","CollectionProperties":[{"Name":"CPU Load Percentage","Value":{"ValueKind":4}}]}]

Beitnes answered 10/12, 2020 at 13:42 Comment(1)
Don't use an object as a property type. Use the type you expect instead, in this case int. What you posted is JSON, not integers. Since you explicitly wrote that you don't want that payload parsed, the deserializer returned what the input contained, a JTokenClaudicant
B
4

I had the same issue, the only way I found was to use System.Text.Json for serializing :

string json = System.Text.Json.JsonSerializer.Serialize(resultPacket);

The ValueKind property comes when using dynamic objects and expandoObject in my case. Hope this helps!

Ballottement answered 11/5, 2022 at 11:53 Comment(1)
It works - thank you. Couple hours couldn't pass "this exam")))Garaway
R
1

Assuming this is a valid incoming json,

[{"Name":"CPU Load Percentage","Value":{"ValueKind":4}}]

Change your class to look like this. You can change the class /property name as per your need. Json Attribute is to map your property with incoming JSON property. Also, you can use dynamic or JObject if your incoming JSON property can change in future. If it wont, you can use the code below:

        public class Value    {
            [JsonProperty("ValueKind")]
            public int ValueKind; 
        }
    
        public class SystemData{
            [JsonProperty("Name")]
            public string Name; 
    
            [JsonProperty("Value")]
            public Value Value; 
        }
    
//Your root class. 
        public class CollectionProperty{
           
            public List<SystemData> SystemDataList; 
        }

Update

Since your json has [{}] that means , its an array or collection of complex objects. {} = class so it seems, there comes a list of class with two properties Name and Value in which again Value is a class or complex object with a property ValueKind of type int.

Usage

CollectionProperty myDeserializedClass = JsonConvert.DeserializeObject<CollectionProperty>(myJsonResponse);
Responser answered 10/12, 2020 at 13:44 Comment(3)
Thanks, but NOT getting. Could you please explain more, how to design my CollectionProperty classBeitnes
Root will be replaced by CollectionPropertyResponser
I checked this. it's coming nullBeitnes
H
1

This happens when you are mixing serializers. I had this issue as well. Basically, I was deserializing an object using System.Text.Json and then a 3rd party library we were using was serializing the objects using Newtonsoft.Json. This will yield the result that you are seeing.

In my case, since I was using a 3rd party library I could not change how they were serializing, so I basically just made a function to turn all of my class values into a string before serializing it again. This might not be possible for everyone, but since I know my data can be converted to a string, that is how I approached this.

Hellhound answered 19/6, 2024 at 18:58 Comment(0)
D
0

Looks like the issue came from mix of multiple JSON serializers, and System.Text.Json seemed to be the issue in my case.

I was using Newtonsoft.Json for all JSON handling, but request data to controllers were already coming with the ValueKind thing whenever the model had anything with type Object, which was messing up things in the code.

To fix this, I made Newtonsoft.Json the default JSON serializer for the controllers as well

  1. Install NuGet package Microsoft.AspNetCore.Mvc.NewtonsoftJson
  2. In Program.cs configure
builder.Services.AddControllers().AddNewtonsoftJson();
Disgusting answered 30/8, 2024 at 8:13 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.