用c#实现json解析与序列化及格式化输出

1. 简介     

  json(javascript object notation)是一种使用可读文本形式的文件格式,用于传输由key-value对和array数组形式的数据对象。这种数据格式在异步的浏览器-服务端通信模式中经常使用,作为替换xml在ajax中使用。         (摘自wiki,实际上现在的web开发中ajax传输大部分都是使用了json格式文本)

      json对象可以看做是一个嵌套存在的object-array-element对象结构。各元素组成格式略为下方列表

     array: [element, element, ......]                    //数组array是element的一个序列集合,可为空[]

     object : {key-value, key-value, ....}                //对象object是key-value对的一个序列集合,可为空{}

     element: null | bool | int | double | string | object | array 

     key:string                                                   //key只能是字符串形式,本文限定为只包含[0-9],[a-z],[A-Z],-,_字符序列

     value : element                                             //value可以为一切基本元素

2. json文本解析

   json文本解析可以按照上述,根据格式分别解析element,object,array

  使用通用object节点存储每一个element元素,类型使用一个枚举类定义

public enum JsonType
{
    TYPE_NULL = 0,
    TYPE_BOOL = 1,
    TYPE_INT = 2,
    TYPE_DOUBLE = 3,
    TYPE_STRING = 4,
    TYPE_OBJECT_NULL = 10,  //{}
    TYPE_OBJECT = 11,
    TYPE_ARRAY = 12,
}

   节点属性结构初始化如下

    //包含的数据,包括类型,数值,字符串
    public JsonType _type = JsonType.TYPE_NULL;             //默认节点类型为null
    public int _num = 0;                                    //用于存储bool型和int型数据
    public double _double = 0.0f;                           //用于存储double类型数据
    public string _string = "";                             //用于存储string类型数据
    public List<MyJson> list = new List<MyJson>(0);         //用于类型为array的节点存储子节点元素
    public Dictionary<string, MyJson> child = new Dictionary<string, MyJson>();     //用于object存储key-value类型

  1. 解析element

  a. null格式

    public static MyJson ParseNull(string s)
    {
        if (s == "null")
            return new MyJson();
        return null;
    }

 b. bool格式

    public static MyJson ParseBool(string s)
    {
        if (s == "true" || s == "false")
        {
            MyJson tmp = new MyJson();
            tmp._type = JsonType.TYPE_BOOL;
            if (s == "true")
            {
                tmp._num = 1;
            }
            else
            {
                tmp._num = 0;
            }
            return tmp;
        }
        return null;
    }
 c. 解析int类型
    public static MyJson ParseInt(string s)
    {
        int index = 0;
        int num = 0;
        bool flag = false;  //是否有负号
        if (s[0] == '+' || s[0] == '-')
        {
            flag = s[0] == '-';
            index++;
        }
        for (int i = index; i < s.Length; ++i)
        {
            if (s[i] > '9' || s[i] < '0')
                return null;
            num = num * 10 + s[i] - '0';
        }
        MyJson tmp = new MyJson();
        tmp._type = JsonType.TYPE_INT;
        tmp._num = flag ? -1 * num : num;
        return tmp;
    }
 d. 解析double类型
    public static MyJson ParseDouble(string s)
    {
        //校验下是否是double
        int index = 0, cnt = 0;
        if (s[0] == '+' || s[0] == '-') index++;
        for (int i = index; i < s.Length; ++i)
        {
            if (s[i] == '.')
                cnt++;
            else if (s[i] > '9' || s[i] < '0')
                return null;
        }
        if (cnt > 1)
            return null;

        double num = System.Convert.ToDouble(s);
        MyJson tmp = new MyJson();
        tmp._type = JsonType.TYPE_DOUBLE;
        tmp._double = num;
        return tmp;
    }
 e. 解析string类型
    public static MyJson ParseString(string s)
    {
        //判定是否有效字符串
        if (s.Length < 2 || s[0] != '\"' || s[s.Length - 1] != '\"')
            return null;
        MyJson tmp = new MyJson();
        tmp._type = JsonType.TYPE_STRING;
        tmp._string = s.Substring(1, s.Length - 2);
        return tmp;
    }

 f. 解析空对象类型

    public static MyJson ParseNullObject(string s)
    {
        if (s != "{}")
            return null;
        MyJson tmp = new MyJson();
        tmp._type = JsonType.TYPE_OBJECT_NULL;
        return tmp;
    }

 对去格式化文本整体解析代码如下(即json文本已处理过,不包含多余的空格,tab,换行以及其他空白可打印字符等)

    //对象,格式必须为{}开头结尾,中间用,隔开的key:value对
    public static MyJson ParseObject(string s)
    {
        if (s.Length == 0)
            return null;
        if (s == "null")
        {
            return ParseNull(s);
        }
        if (s == "true" || s == "false")
        {
            return ParseBool(s);
        }
        if (s == "{}")
        {
            return ParseNullObject(s);
        }
        if (s[0] == '+' || s[0] == '-' || s[0] >= '0' && s[0] <= '9')
        {
            if (s.IndexOf('.') >= 0)
                return ParseDouble(s);
            return ParseInt(s);
        }
        if (s[0] == '"')
            return ParseString(s);
        if (s.Length < 2)
            return null;
        if (s[0] == '[' && s[s.Length - 1] == ']')
        {
            MyJson tmp = new MyJson();
            tmp._type = JsonType.TYPE_ARRAY;
            int start = 1, end = 1;
            while (end < s.Length - 1)
            {
                while (end < s.Length - 1)
                {
                    if (s[end] == '"')
                    {
                        ++end;
                        while (end < s.Length - 1 && s[end] != '"') ++end;
                    }
                    if (s[end] == '[')
                    {
                        ++end;
                        while (end < s.Length - 1 && s[end] != ']') ++end;
                    }
                    if (s[end] == '{')
                    {
                        ++end;
                        while (end < s.Length - 1 && s[end] != '}') ++end;
                    }
                    if (end == s.Length - 1 || s[end] == ',')
                        break;
                    ++end;
                }
                MyJson subtmp = ParseObject(s.Substring(start, end - start));
                tmp.list.Add(subtmp);
                end++;
                start = end;
            }
            return tmp;
        }
        if (s[0] == '{' && s[s.Length - 1] == '}')
        {

            MyJson tmp = new MyJson();
            tmp._type = JsonType.TYPE_OBJECT;
            int start = 1, end = 1;
            while (end < s.Length - 1)   //到末尾
            {
                while (end < s.Length - 1)   //到逗号或者末尾,一对key:value
                {
                    if (s[end] == '"')
                    {
                        ++end;
                        while (end < s.Length - 1 && s[end] != '"') ++end;
                    }
                    if (s[end] == '[')
                    {
                        ++end;
                        while (end < s.Length - 1 && s[end] != ']') ++end;
                    }
                    if (s[end] == '{')
                    {
                        ++end;
                        while (end < s.Length - 1 && s[end] != '}') ++end;
                    }
                    if (end == s.Length - 1 || s[end] == ',')  //真实逗号,不是在引号里面的
                        break;
                    ++end;
                }
                string sub = s.Substring(start, end - start);
                string key;
                MyJson subtmp = ParseKeyValue(sub, out key);
                if (subtmp == null)
                    return null;
                if (tmp.child.ContainsKey(key))
                    return null;
                tmp.child.Add(key, subtmp);
                end++;
                start = end;
            }
            return tmp;
        }
        return null;
    }

  这里有两个引用方法,一个是解析key-value对的ParseKeyValue,一个是校验json对象的key是否是合法字符串CheckKeyValid,代码分别如下

    public static MyJson ParseKeyValue(string s, out string key)
    {
        key = "";
        //获取key, value
        if (s[0] != '"')
            return null;
        int index = 1;
        while (index < s.Length && s[index] != '"') ++index;
        if (index == s.Length)
            return null;
        key = s.Substring(1, index - 1);
        //校验key是否合法
        if (!CheckKeyValid(key))
        {
            return null;
        }
        //取出后面的value来
        index++;
        if (index == s.Length || s[index] != ':')
            return null;
        index++;

        string sValue = s.Substring(index, s.Length - index);
        MyJson val = ParseObject(sValue);

        return val;
    }
    public static bool CheckKeyValid(string s)    //在key内只允许存在a-z,A-Z,0-9,-,_
    {
        if (s.Length == 0) return false;
        for (int i = 0; i < s.Length; ++i)
        {
            if (s[i] != '-' && s[i] != '_' && !(s[i] >= '0' && s[i] <= '9') && !(s[i] >= 'a' && s[i] <= 'z') && !(s[i] >= 'A' && s[i] <= 'Z'))
                return false;
        }
        return true;
    }
 g. json文本的预处理(去除多余空格,换行)
    public static string CondenceString(string s)
    {
        //主要是压缩非\"\"字符串之间的其他符号
        System.IO.StringWriter sw = new System.IO.StringWriter();
        int index = 0;
        while (index < s.Length)
        {
            if (s[index] == '\"')
            {
                sw.Write(s[index]);
                ++index;
                while (index < s.Length && s[index] != '\"')
                {
                    sw.Write(s[index]);
                    ++index;
                }
            }
            //在tab内
            while (index < s.Length && tab.IndexOf(s[index]) >= 0)
                ++index;
            if (index == s.Length) break;
            sw.Write(s[index]);
            ++index;
        }
        return sw.ToString();
    }

  这里可以预声明哪些符号是可以省略的,如本文定义为    const string tab = " \t\n\b";(空格,tab,换行,\b可以忽略),处理的主要是在""包含字符串外的这些字符.

    至此,文本转化为json对象的方法已经处理完。

3. json序列化及格式化

json的序列化可以看做是解析的反过程,使用递归方式对object做递归序列化为字符串即可,代码如下:

    public string ToStr()
    {
        if (_type == JsonType.TYPE_NULL)
        {
            return "null";
        }
        if (_type == JsonType.TYPE_BOOL)
        {
            return _num == 1 ? "true" : "false";
        }
        if (_type == JsonType.TYPE_INT)
        {
            return _num.ToString();
        }
        if (_type == JsonType.TYPE_DOUBLE)
        {
            return _double.ToString();
        }
        if (_type == JsonType.TYPE_STRING)
        {
            return "\"" + _string + "\"";
        }
        if (_type == JsonType.TYPE_OBJECT_NULL)
        {
            return "{}";
        }
        if (_type == JsonType.TYPE_OBJECT)
        {
            System.IO.StringWriter sw = new System.IO.StringWriter();
            sw.Write("{");
            int cnt = 0;
            foreach (var tmp in child)
            {
                sw.Write("\"");
                sw.Write(tmp.Key);
                sw.Write("\":");
                sw.Write(tmp.Value.ToStr());
                if (++cnt < child.Count)
                    sw.Write(",");
            }
            sw.Write("}");
            return sw.ToString();
        }
        if (_type == JsonType.TYPE_ARRAY)
        {
            System.IO.StringWriter sw = new System.IO.StringWriter();
            sw.Write("[");
            int cnt = 0;
            foreach (var tmp in list)
            {
                sw.Write(tmp.ToStr());
                if (++cnt < list.Count)
                    sw.Write(",");
            }
            sw.Write("]");
            return sw.ToString();
        }
        return "";
    }
带有格式化的输出,本文主要是处理换行和缩进关系,预声明对array的格式化输出形式和object的格式化输出形式的空格缩进
    const string objtab = "  ";
    const string arraytab = "  ";
格式化输出的代码如下
   public string ToStrWithFormat()
    {
        if (_type == JsonType.TYPE_NULL)
        {
            return "null";
        }
        if (_type == JsonType.TYPE_BOOL)
        {
            return _num == 1 ? "true" : "false";
        }
        if (_type == JsonType.TYPE_INT)
        {
            return _num.ToString();
        }
        if (_type == JsonType.TYPE_DOUBLE)
        {
            return _double.ToString();
        }
        if (_type == JsonType.TYPE_STRING)
        {
            return "\"" + _string + "\"";
        }
        if (_type == JsonType.TYPE_OBJECT_NULL)
        {
            return "{}";
        }
        if (_type == JsonType.TYPE_OBJECT)
        {
            System.IO.StringWriter sw = new System.IO.StringWriter();
            sw.Write("{\n");
            int cnt = 0;
            foreach (var tmp in child)
            {
                sw.Write(objtab + "\"");
                sw.Write(tmp.Key);
                sw.Write("\":");
                string sub = tmp.Value.ToStrWithFormat();
                Debug.Log("sub" + sub);
                string[] slist = tmp.Value.ToStrWithFormat().Split('\n');
                Debug.Log("slist:" + slist.Length);
                sw.Write(slist[0]);
                if (slist.Length > 2)
                {
                    for (int i = 1; i < slist.Length - 1; ++i)
                        sw.Write("\n" + objtab + slist[i]);
                    sw.Write("\n" + objtab + slist[slist.Length - 1]);
                }
                if (++cnt < child.Count)
                    sw.Write(",");
                sw.Write("\n");
            }
            sw.Write("}");
            return sw.ToString();
        }
        if (_type == JsonType.TYPE_ARRAY)
        {
            System.IO.StringWriter sw = new System.IO.StringWriter();
            sw.Write("[\n");
            int cnt = 0;
            foreach (var tmp in list)
            {
                sw.Write(arraytab);
                //sw.Write(tmp.ToStr());
                string[] slist = tmp.ToStrWithFormat().Split('\n');
                sw.Write(slist[0]);
                if (slist.Length > 2)
                {
                    for (int i = 1; i < slist.Length - 1; ++i)
                        sw.Write("\n" + arraytab + slist[i]);
                    sw.Write("\n" + arraytab + slist[slist.Length - 1]);
                }
                if (++cnt < list.Count)
                    sw.Write(",");
                sw.Write("\n");
            }
            sw.Write("]");
            return sw.ToString();
        }
        return "";
    }
4. json对象的类型判定、基本元素值获取,根据key下标取值等方法

这些都是基本的处理方式,本文不予详解了,感兴趣的可以点击链接我的码云查看



本想使用c++实现这一功能,发现自己最近工作c#脚本写多了,写c++总有些卡壳,不如这边方便,于是便改用c#在unity下实现了这一功能,但是目前只有一些基本的功能,可能还有不少bug特殊情况未考虑到,比如说对json文本内注释的处理,对字符串中特殊字符、转义字符的处理等。这些之后会不断修改增加。

这份代码编写原因主要是阅读了大牛的一个json开源代码cjson,使用c实现,通过理解其对json对象的处理基本方式,自己也写了个,不得不说,使用高级语言编写,不需要操作内存的获取释放,真的是编写难度要小很多,但是同时运行效率上应该也会差不少。



以后也要经常写写自己游戏开发的一些学习内容、体验,不断学习,不断进步吧。

猜你喜欢

转载自blog.csdn.net/u013434984/article/details/80305979