Exemplo n.º 1
0
        public static void SerializeJsonMapNoDefaults(string name, OSD osd, ref int mapcont, osUTF8 sb)
        {
            int i;

            switch (osd.Type)
            {
            case OSDType.Boolean:
                bool ob = osd.AsBoolean();
                if (ob)
                {
                    if (mapcont++ > 0)
                    {
                        sb.AppendASCII(',');
                    }
                    appendJsonString(name, sb);
                    sb.AppendASCII(':');
                    sb.Append(ob ? osUTF8Const.OSUTF8true : osUTF8Const.OSUTF8false);
                }
                break;

            case OSDType.Integer:
                int oi = osd.AsInteger();
                if (oi != 0)
                {
                    if (mapcont++ > 0)
                    {
                        sb.AppendASCII(',');
                    }
                    appendJsonString(name, sb);
                    sb.AppendASCII(':');
                    sb.AppendInt(oi);
                }
                break;

            case OSDType.Real:
                double od = osd.AsReal();
                if (od != 0)
                {
                    if (mapcont++ > 0)
                    {
                        sb.AppendASCII(',');
                    }
                    appendJsonString(name, sb);
                    sb.AppendASCII(':');
                    string str = Convert.ToString(od, NumberFormatInfo.InvariantInfo);
                    sb.AppendASCII(str);

                    if (str.IndexOfAny(new char[] { '.', 'E' }) == -1)
                    {
                        sb.AppendASCII(".0");
                    }
                }
                break;

            case OSDType.String:
            case OSDType.URI:
            case OSDType.LLSDxml:
                string ostr = osd.AsString();
                if (!string.IsNullOrEmpty(ostr))
                {
                    if (mapcont++ > 0)
                    {
                        sb.AppendASCII(',');
                    }
                    appendJsonString(name, sb);
                    sb.AppendASCII(':');
                    appendJsonString(ostr, sb);
                }
                break;

            case OSDType.OSDUTF8:
                osUTF8 ou8 = ((OSDUTF8)osd).value;
                if (ou8 != null && ou8.Length > 0)
                {
                    if (mapcont++ > 0)
                    {
                        sb.AppendASCII(',');
                    }
                    appendJsonString(name, sb);
                    sb.AppendASCII(':');
                    appendJsonOSUTF8(ou8, sb);
                }
                break;

            case OSDType.UUID:
                UUID ou = osd.AsUUID();
                if (ou.IsNotZero())
                {
                    if (mapcont++ > 0)
                    {
                        sb.AppendASCII(',');
                    }
                    appendJsonString(name, sb);
                    sb.AppendASCII(":\"");
                    sb.AppendUUID(ou);
                    sb.AppendASCII('"');
                }
                break;

            case OSDType.Date:
                if (mapcont++ > 0)
                {
                    sb.AppendASCII(',');
                }
                appendJsonString(name, sb);
                sb.AppendASCII(':');
                appendJsonString(osd.AsString(), sb);
                break;

            case OSDType.Binary:
                byte[] binary = osd.AsBinary();
                if (mapcont++ > 0)
                {
                    sb.AppendASCII(',');
                }
                appendJsonString(name, sb);
                sb.AppendASCII(":[");
                if (binary != null && binary.Length > 0)
                {
                    i = 0;
                    while (i < binary.Length - 1)
                    {
                        sb.AppendInt(binary[i++]);
                        sb.AppendASCII(',');
                    }
                    if (i < binary.Length)
                    {
                        sb.AppendInt(binary[i]);
                    }
                }
                sb.AppendASCII(']');
                break;

            case OSDType.Array:
                if (mapcont++ > 0)
                {
                    sb.AppendASCII(",");
                }
                appendJsonString(name, sb);
                sb.AppendASCII(":[");

                OSDArray array = (OSDArray)osd;
                if (array != null && array.Count > 0)
                {
                    i = 0;
                    while (i < array.Count - 1)
                    {
                        SerializeJson(array[i++], sb, false);
                        sb.AppendASCII(',');
                    }
                    if (i < array.Count)
                    {
                        SerializeJson(array[i], sb, false);
                    }
                }
                sb.AppendASCII(']');
                break;

            case OSDType.Map:
                OSDMap map = (OSDMap)osd;
                if (map != null && map.Count > 0)
                {
                    if (mapcont++ > 0)
                    {
                        sb.AppendASCII(',');
                    }
                    appendJsonString(name, sb);
                    sb.AppendASCII(":{");

                    i = 0;
                    foreach (KeyValuePair <string, OSD> kvp in map)
                    {
                        SerializeJsonMapNoDefaults(kvp.Key, kvp.Value, ref i, sb);
                    }
                    sb.AppendASCII('}');
                }
                break;

            case OSDType.Unknown:
            default:
                break;
            }
        }