在服务堆栈中对序列化/序列化JSON进行定制 [英] Costomising the serialisation/serialised JSON in service stack

查看:149
本文介绍了在服务堆栈中对序列化/序列化JSON进行定制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从Service Stack中转换此beutifull默认JSON:

I need to turn this beutifull default JSON from Service Stack :

{
      "Status": "ok",
      "LanguageArray": [{
      "Id": 1,
      "Name": "English"
      }, {
      "Id": 2,
      "Name": "Chinese"
      }, {
      "Id": 3,
      "Name": "Portuguese"
      }]
     }

对于这种可憎的怪兽:

{"status":"ok","language":{
     "0":{"id":"1", "name":"English"},
     "1":{"id":"2", "name":"Chinese"},
     "2":{"id":"3", "name":"Portuguese"},
     }

出于我无法控制,无法控制或不合理的原因.

For reasons beyond my control or ration or logic.

我的问题是实现这一目标的最简单方法是什么? 我需要对几乎所有数组执行此操作(它们是使用C#中的List实现的IEnumerables)

My question is what are the simplest way of achieving this? I need to do this to almost all Arrays ( they are IEnumerables implemented using List in C#)

推荐答案

我认为我没有最简单的方法",它取决于您需要在何处进行序列化以及格式的严格程度(是否区分大小写?,空格?)

I don't think I have a 'simplest way' and it kind of depends on where you need the serialization to happen as well as how strict the format is (case sensitive?, spacing?)

如果放置示例文本并将其变成这样的类

If place your example text and turn it into classes like this

public class Language
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class OuterLanguage
{
    public string Status { get; set; }
    public IEnumerable<Language> LanguageArray { get; set; }
}

一种非常直接的方法是拥有一个Dto,该Dto添加用于Array序列化的属性,而忽略原始Array.

A pretty straightforward way would be having a Dto that adds a property for serialization of the Array and ignores the original Array.

public class OuterLanguageDto
{
    public string Status { get; set; }
    [IgnoreDataMember]
    public IEnumerable<Language> LanguageArray { get; set; }
    public IDictionary<string, Language> language 
    { 
        get { return this.LanguageArray.ToDictionary(x => (x.Id - 1).ToString()); } 
    }
}

当需要序列化类(OuterLanguage)时,可以使用TranslateTo(或首选的Mapper)并将值复制到Dto.

When you need to serialize your class (OuterLanguage) you use TranslateTo (or your preferred Mapper) and copy the values over to the Dto.

var jsonObj = obj.TranslateTo<OuterLanguageDto>();
var jsonStr = new JsonSerializer<OuterLanguageDto>().SerializeToString(jsonObj);

您还可以查看自定义序列化.这将使您有一种方法可以将所有阵列序列化为正确的格式.

You could also look into Custom Serialization. That would give you to have a way to serialize all your Arrays into the correct format.

JsConfig<IEnumerable<Language>>.SerializeFn = ToJson

private string ToJson(IEnumerable<Language> arr)
{
    var dict = arr.ToDictionary(x => x.Id - 1);

    var str = JsonSerializer.SerializeToString<Dictionary<int, Language>>(dict);

    return str;
}

但是,我猜测您可能需要做一些魔术字符串工作",以获取自定义序列化数组的json字符串,并在外部类(在此示例中为OuterLanguage)被序列化时正确格式化它的json字符串.

However, I'm guessing you may have to do some "magic string work" to take your custom serialized Array's json string and format it properly when outer class (OuterLanguage in this example) is the one being serialized.

这篇关于在服务堆栈中对序列化/序列化JSON进行定制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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