替换为"null"到"0"在json中仅用于int还是使用Nullable< int&gt ;? [英] Replace "null" to "0" in json only for int or use Nullable<int>?

查看:40
本文介绍了替换为"null"到"0"在json中仅用于int还是使用Nullable< int&gt ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将尝试描述我的处境:

I'll try to describe my situation:

我有Windows Store App(C#),该应用程序从远程数据库( DataBase1 )接收数据,并将其存储在隔离存储( DataBase2 )的本地数据库中.我有一个单独的类,用于所有操作,例如ex的数据:

I have Windows Store App (C#), which receive data from remote data base (DataBase1), and store it in local database in isolated storage (DataBase2). I have single class for all manipulation with data for ex:

[Table("Table")]
[DataContract]
public class Class1
{
    [PrimaryKey]
    [Column("id")]
    [DataMember(Name = "id")]
    public int Id { get; set; } 
}

该课程可容纳50多个字段.
我使用 Newtonsoft.Json :

This class can take 50+ fields.
I use Newtonsoft.Json:

 var class = JsonConvert.DeserializeObjectAsync<Class1>(json);

一切正常,但是在 DataBase1 中,所有字段都具有默认值= null ,当我收到null时,我会遇到异常.

All works great, but in DataBase1 all fields have default value = null, and when I receive null I have exception.

我有2个解决方法:

  1. 制作 int ID -> int?id ,然后在setter中将"null"更改为"0"(并且始终使用nulleable类型),但是我不确定性能.
  2. 创建另一个可为空的字段( int?IdCanBeNull ),在其中存储DataBase1中的值,然后在 IdCanBeNull 的setter中将嵌套值设置为 Id 并始终与 int Id (不可为null的类型)一起使用(将其保存在 DataBase2 等中).性能更好,但需要创建2xNumberOfFields
  1. make int Id -> int? Id, and in setter change "null" to "0" (and always work with nulleable type), but I'm not sure about performance.
  2. make another nullable field (int? IdCanBeNull), where store value from DataBase1, then in IdCanBeNulls setter set nested value to Id and always work with int Id (not nullable type) (save it in DataBase2 and other). Better performance, but need to create 2xNumberOfFields

我的问题是:
哪种方法正确?
也许您还有另一个想法,如何使用 Newtonsoft.Json 将"null"存储为"0".>尝试解释您的答案.

My questions is:
Which is right way?
May be you have another idea, how to store "null" to "0" with using Newtonsoft.Json.
Try to explain your answer.

注意:

  1. 是的,当我收到json时,我可以将所有的"null"都删除为"0"-但这不是一个好的解决方案.
  2. 我无法更改 DataBase1 的默认值.

推荐答案

还有第三种方法.您可以为此编写一个转换器类

There is a third way. You can write a converter class for this

var list = JsonConvert.DeserializeObject<List<Class1>>(
                            "[{Id:5},{Id:0},{Id:null},{}]",
                             new MyConverter());


public class Class1
{
    public int Id { get; set; }
}

public class MyConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(int);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (reader.Value == null) return 0;
        return Convert.ToInt32(reader.Value);
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

这篇关于替换为"null"到"0"在json中仅用于int还是使用Nullable&lt; int&gt ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆