Serialize, desirialize Utf8 Json's Decimal in C#.netcore

Asked 2 months ago, Updated 2 months ago, 6 views

If you serialize the Decimal value, you will receive a Double value. Resolver, Formatter, IJsonFormatterResolver, IJsonFormatter<> seems to be used to solve this problem, but I don't know how to write it.I would like to ask for a solution if it is a sample or an example implementation.

using Microsoft.CodeAnalysis.CSharp.Scripting;
using System;
using System.Diagnostics;
using Microsoft.CodeAnalysis.Scripting;
using System.IO;
namespace serialization test {
 public class program {
     static void Main() {
         const String filename = @"Json.json";
         varScript = CsharpScript.Create(
             "0.11111111111111111m",
             ScriptOptions.Default.AddReferences(typeof(Object).Assembly)
             .AddImports ("System"),
             type of (Object)
         );
         vare=Script.CreateDelegate()("");
         e.Wait();
         var input = e.Result;
         using(varf=newFileStream(filename,FileMode.Create)){
             Utf8Json.JsonSerializer.Serialize(f, input);
         }
         using(varf=newFileStream(filename,FileMode.Open)){
             var output = Utf8Json.JsonSerializer.Deserialize<Object>(f);
             Console.WriteLine($"{input.GetType()}{input}");
             Console.WriteLine($"{output.GetType()}{output}";
             Debug.Assert(input.GetType()==typeof(Decimal));
             Debug.Assert(output.GetType()==typeof(Decimal)); // error output.GetType()==typeof(Double)
         }
         Console.ReadKey();
     }
 }
}

c# json .net-core

2022-09-30 11:37

2 Answers

This test program will be helpful.
Utf8Json/tests/Utf8Json.Tests/DecimalTest.cs

vard=decimal.MaxValue;
varbin=JsonSerializer.Serialize(d);
JsonSerializer.Deserialize<decimal>(bin).Is(d);

You will need to specify the type explicitly during the de-serialization.
The source of the question should be as follows:

var output=Utf8Json.JsonSerializer.Deserialize<Object>(f);

Specify <decimal> instead of <Object>.

var output=Utf8Json.JsonSerializer.Deserialize<decimal>(f);

If you explicitly specify the type of variable in the class in the same test program description, it seems to work.

public class Foo
{
    public decimal bar {get;set;}
    public string More {get;set;}
}

var foo=JsonSerializer.Serialize(new Foo {Bar=-31.42323m, More="mmmm"});
vardd=JsonSerializer.Deserialize<Foo>(foo);
ddd.Bar.Is (-31.42323m);
ddd.More.Is("mmmmmm");


2022-09-30 11:37

Thank you.I didn't know what would happen if the members had a Decimal instead of a simple Decimal, but it was helpful that you answered clearly.
I want to receive it anonymously, so I checked it with the code to receive the dummy arguments in generic and de-serialize them.

using Microsoft.CodeAnalysis.CSharp.Scripting;
using System;
using System.Diagnostics;
using Microsoft.CodeAnalysis.Scripting;
using System.IO;
namespace serialization test {
    public class program {
        const String filename = @"Json.json";
        static T Desirize <T> (T dummy) {
            using(varf=newFileStream(filename,FileMode.Open)){
                var output = Utf8Json.JsonSerializer.Deserialize<T>(f);
                return output;
            }
        }
        static void Main() {
            varScript = CsharpScript.Create(
                "new {a=1m, b=2m}",
                ScriptOptions.Default.AddReferences(typeof(Object).Assembly)
                .AddImports ("System"),
                type of (Object)
            );
            vare=Script.CreateDelegate()("");
            e.Wait();
            var input = e.Result;
            using(varf=newFileStream(filename,FileMode.Create)){
                Utf8Json.JsonSerializer.Serialize(f, input);
            }
            var output = Desirize (new {a=0m, b=0m});
            Console.WriteLine($"{input.GetType()}{input}");
            Console.WriteLine($"{output.GetType()}{output}";
            Console.ReadKey();
        }
    }
}


2022-09-30 11:37

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.