0

passing a Json value like this one(this will be the var jsonValue in code):

"{\"Something\":0,\"Something2\":10,\"Something3\":{\"Something4\":17,\"Something5\":38042,\"Something6\":38043,\"Id\":215},\"Something7\":215,\"SomethingId\":42,\"Something8\":\"AString, Gläser\",\"Something8\":\"44-55-18\",\"Status\":{\"Caption\":\"Fixed\",\"Value\":7},\"Type\":\"Article\",\"Id\":97,\"@Delete\":true,\"Something9\":\"8\"}"

to the following code:

var deserializer = new JsonSerializer();
const string regex = @"/Date\((.*?)\+(.*?)\)/";
var reader = new JsonTextReader(new StringReader(jsonValue));
returnValue = deserializer.Deserialize(reader, type);

type is the typeof https://dotnetfiddle.net/LMPEl0 (thank you Craig) (sorry for the weird names, can't disclose the actual ones...)

The jsonvalue is generated by input in an editable cell of a DataTable and apparently places previously null values in the end of the json string.

I get a null value in the "Something9" property in the returnValue, instead of 8(Something9 was null before and set to 8 through an editable Cell of a DataTable) Is there some problem with the Json value that I can't see? Or do I need some setting in the Deserializer?

Thanks

jimmious
  • 358
  • 3
  • 19
  • Something9 contains string, maybe your class have Something9 as Integer and it does not parse it..? – libik Nov 13 '15 at 15:13
  • Indeed it is an integer in the class. I should change the input to integer you believe? – jimmious Nov 13 '15 at 15:17
  • What is "type" in your code snippet? – Volkan Paksoy Nov 13 '15 at 15:18
  • @DimitrisG. - yes, change it either in code or in json. – libik Nov 13 '15 at 15:19
  • Sorry guys, type is in this example {Name = "PositionDto" FullName = "MyProject.Contracts.Data.Events.PositionDto"} Also an important notice is that Something9 as a property is normally after "Something2" but for some reason when the json object is created, it gets put in the end, which COULD cause the issue. I will add this info to the question. Thank you all for the fast answers too! – jimmious Nov 13 '15 at 15:25
  • If its an integer how can it be null? Does something9 has a public setter? – Ralf Nov 13 '15 at 15:34
  • public int? Something9 { get; set; } The value is passed in it from an editable cell of a DataTable – jimmious Nov 13 '15 at 15:34

2 Answers2

3

You don't show what your type is so I generated one using http://json2csharp.com.

public class Something3
{
    public int Something4 { get; set; }
    public int Something5 { get; set; }
    public int Something6 { get; set; }
    public int Id { get; set; }
}

public class Status
{
    public string Caption { get; set; }
    public int Value { get; set; }
}

public class RootObject
{
    public int Something { get; set; }
    public int Something2 { get; set; }
    public Something3 Something3 { get; set; }
    public int Something7 { get; set; }
    public int SomethingId { get; set; }
    public string Something8 { get; set; }
    public Status Status { get; set; }
    public string Type { get; set; }
    public int Id { get; set; }
    [JsonProperty("@Delete")]
    public bool Delete { get; set; }
    public string Something9 { get; set; }
}

Because one of your properties has a name that is not valid as a .NET property I added the [JsonProperty] attribute to that one. After that it worked perfectly. Perhaps the problem is with how you declared the @Delete JSON property in your .NET type. Given that Something9 comes after that property it would be my guess that that's part of the problem.

Here's the fiddle.

https://dotnetfiddle.net/McZF9Q

Craig W.
  • 17,838
  • 6
  • 49
  • 82
  • Hello Craig. Thank you for the answer. The type is like you have it in the fiddle with the exception of "Something9" following "Something2". When the json value is generated though, it gets placed in the end of the string(because it had a null value before?). Also "Something9" is a public int? and gets the new value passed in an editable cell of a DataTable. – jimmious Nov 13 '15 at 15:30
  • Also, the @Delete property is the Delete column of the table and gets passed like that in the json string. I should probably mention that the problem does not occur when the value of Something9 is NOT null before and changes. – jimmious Nov 13 '15 at 15:46
  • Ok finally I found the problem and while your answer was not directly solving the issue, it helped a lot to the right direction so I will accept it as correct. For the history, the problem was the Status object, which was in reality an Enum and was not Deserializing correctly (I had to implement a custom Deserializer for Enums) therefore "destroying" any properties in the json string after it. – jimmious Nov 13 '15 at 17:04
1

While Craig's answer helped a lot and finally led to a solution the exact answer to the problem was the following:

The Status object is an Enum and was not Deserialized correctly. Due to that, anything that followed in the Json string was also not deserialized.

Implementing a custom Enum Deserializer was the solution. There are other Questions in stackoverflow that helped with this, particularly this one here: How can I ignore unknown enum values during json deserialization?

Thank you everyone :)

Community
  • 1
  • 1
jimmious
  • 358
  • 3
  • 19