Problem with JsonConvert.SerializeObject

Please post public support tickets here. Note: for private support tickets, please send an email to support@cshtml5.com instead.
Black-Byte
Posts: 15
Joined: Fri Jan 29, 2016 2:05 am

Problem with JsonConvert.SerializeObject

Postby Black-Byte » Tue Mar 26, 2019 7:42 am

Hi,

i'm testing with the new 2.0 preview release and with the addon jsonconvert from https://gist.github.com/cshtml5/2446871 ... 448b6aca54.

i've no problem to deserialize but simple serialize does only return an empy value.

Code: Select all

   
      internal void check();
      {      
         test x = new test();
         string temp = JsonConvert.SerializeObject(x, true);
      }

      public class test
      {
         public string A = "AAA";
         public string B = "BBB";
      }


In temp i get only {} so it looks like something is wrong. Could someone help me ?

Greets

Black-Byte
Posts: 15
Joined: Fri Jan 29, 2016 2:05 am

Re: Problem with JsonConvert.SerializeObject

Postby Black-Byte » Tue Mar 26, 2019 8:35 am

I've found the problem i must use get/set

Code: Select all

            test x = new test();
            x.A = "AAA";
            x.B = "BBB";
            x.C = test.ENU_TEST.Hello;
            string temp = JsonConvert.SerializeObject(x, true);
            
            
public class test
      {
         public string A { get; set;}
         public string B { get; set;}

         public ENU_TEST C { get; set; }

         public enum  ENU_TEST : Int16
         {
            Hello =1,
            World =2
         }
      }            


But now i get some error when im using DeserializeObject. String.Double could not convert to ENU_TEST ....

Black-Byte
Posts: 15
Joined: Fri Jan 29, 2016 2:05 am

Re: Problem with JsonConvert.SerializeObject

Postby Black-Byte » Wed Mar 27, 2019 8:24 am

I've tested a little more and i found some bug in JsonConvert.cs the SerializeObject converts an enum not correct.

My Example gives me a Return of {"A":"AAA","B":"BBB","C":"Hello"} but thats not correct

I've change some code in JsonConvert.cs
From

Code: Select all

      static object ConvertCSharpObjectToJavaScriptObject(object cSharpObject, bool ignoreErrors)
      {
         if (cSharpObject is Enum || cSharpObject is Guid || cSharpObject is long)
         {
            return cSharpObject.ToString();
         }
         else if (cSharpObject is DateTime)
         ...

To

Code: Select all

      static object ConvertCSharpObjectToJavaScriptObject(object cSharpObject, bool ignoreErrors)
      {
         if (cSharpObject is Guid || cSharpObject is long)
         {
            return cSharpObject.ToString();
         }
         else if (cSharpObject is Enum)
         {
            return Interop.ExecuteJavaScript("$0.v", cSharpObject);
         }
         else if (cSharpObject is DateTime)
         ...


With that change i get {"A":"AAA","B":"BBB","C":1} thats is ok.

But the DeserializeObject does not work well with {Get;Set;} i allways get an conversion error form double to Enum ..
If i remove the {Get;Set} the DeserializeObject has no problems and works well, but the SerializeObject doesn't work.

One way could be to change this one.

Code: Select all

            foreach (PropertyInfo property in cSharpObject.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
            {
               string propertyName = property.Name;
               object propertyValue = property.GetValue(cSharpObject);

               if (propertyValue != null)
               {
                  var recursionResult = ConvertCSharpObjectToJavaScriptObject(propertyValue, ignoreErrors);
                  if (recursionResult != null)
                  {
                     Interop.ExecuteJavaScript(@"$0[$1] = $2;", jsObject, propertyName, recursionResult);
                  }
               }
            }
            

So it would use all fields not only the with {Get;Set;} but i don't know yet how to fix this.

Greets

Black-Byte
Posts: 15
Joined: Fri Jan 29, 2016 2:05 am

Re: Problem with JsonConvert.SerializeObject

Postby Black-Byte » Fri Mar 29, 2019 1:48 am

Looks like that was my selfmade problem. I was used to the replace of an enum to its value. JSON doesn't have enums. So i have undo my changes and use now the direkt replacement and it works. Output -> {"A":"AAA","B":"BBB","C":"Hello"} gives mit with the deserializer no errors

Edit:
I've changed my server code to work around the insufficiency of the JsonConvert Class

Code: Select all

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

[JsonConverter(typeof(StringEnumConverter))]
public ENU_TEST  C { get; set; }


Return to “Technical Support”

Who is online

Users browsing this forum: No registered users and 2 guests

 

 

cron