Esempio n. 1
0
 internal String GetTag(VisualNode3 node, Boolean end = false)
 {
     if (JsonTag.JSON_OBJECT == node.Tag_Viewer)
     {
         if (node.m_JsonNode.HasKey)
         {
             if (end)
             {
                 return($"</{node.Key_Viewer}>");
             }
             else
             {
                 return($"<{node.Key_Viewer}>");
             }
         }
     }
     else if (JsonTag.JSON_STRING == node.Tag_Viewer)
     {
         if (node.m_JsonNode.HasKey)
         {
             return($"<{node.Key_Viewer}>{node.Value_Viewer}</{node.Key_Viewer}>");
         }
     }
     return("");
 }
Esempio n. 2
0
        public void LoadJSON(String fileName)
        {
            JSONdata = File.ReadAllBytes(fileName);
            JsonNode result;
            int      endPos = -1;

            JsonErrno e = jsonParser.Parse(JSONdata, ref endPos, out result);

            if (JsonErrno.OK == e)
            {
                JSON = new VisualNode3(ref result, JSONdata, 10000);
            }
        }
Esempio n. 3
0
        private object DecodeValue()
        {
            var    token = (byte)input.ReadByte();
            Object retVal;
            String value;

            if (token <= Token.MAX)
            {
                return(token);
            }
            JsonNode root = o;

#if DEBUGGING && DoubleLinked
            while (root?.Parent?.Parent != null)
            {
                root = root.Parent;
            }
            VisualNode3 oV = new VisualNode3(ref root, Encoding.UTF8.GetBytes(jsonTxt.ToString()), 10000);
#endif
            switch (token)
            {
            case Token.NULL:
                jsonTxt.Write("null");
                o.Tag = JsonTag.JSON_NULL;
                return(null);

            case Token.TRUE:
                jsonTxt.Write("true");
                o.Tag = JsonTag.JSON_TRUE;
                return(true);

            case Token.FALSE:
                jsonTxt.Write("false");
                o.Tag = JsonTag.JSON_FALSE;
                return(false);

            case Token.EOBJECT:
                o.Tag = JsonTag.JSON_OBJECT;
                return(new Dictionary <string, object>());

            case Token.EARRAY:
                o.Tag = JsonTag.JSON_ARRAY;
                return(new List <object>());

            case Token.ESTRING:
                o.Tag = JsonTag.JSON_STRING;
                return(string.Empty);

            case Token.OBJECT:
                jsonTxt.Write('{');
                retVal = DecodeObject();
                jsonTxt.Write('}');
                return(retVal);

            case Token.ARRAY:
                jsonTxt.Write('[');
                retVal = DecodeArray();
                jsonTxt.Write(']');
                return(retVal);

            case Token.INTEGER:
                retVal = input.ReadVarint32().ZigZagDecode();
                jsonTxt.Write(retVal);
                o.Tag = JsonTag.JSON_NUMBER;
                o.doubleOrString.number = Convert.ToDouble(retVal);
                return(retVal);

            case Token.LONG:
                retVal = input.ReadVarint64().ZigZagDecode();
                jsonTxt.Write(retVal);
                o.Tag = JsonTag.JSON_NUMBER;
                o.doubleOrString.number = Convert.ToDouble(retVal);
                return(retVal);

            case Token.FLOAT:
                if (input.Read(convertArray, 0, 4) != 4)
                {
                    throw new PsonException("stream ended prematurely");
                }
                if (!BitConverter.IsLittleEndian)
                {
                    Array.Reverse(convertArray, 0, 4);
                }
                float retF = BitConverter.ToSingle(convertArray, 0);
                jsonTxt.Write(retF.ToString(System.Globalization.CultureInfo.InvariantCulture));
                o.Tag = JsonTag.JSON_NUMBER;
                o.doubleOrString.number = Convert.ToDouble(retF);
                return(retF);

            case Token.DOUBLE:
                if (input.Read(convertArray, 0, 8) != 8)
                {
                    throw new PsonException("stream ended prematurely");
                }
                if (!BitConverter.IsLittleEndian)
                {
                    Array.Reverse(convertArray, 0, 8);
                }
                double retD = BitConverter.ToDouble(convertArray, 0);
                o.Tag = JsonTag.JSON_NUMBER;
                o.doubleOrString.number = retD;
                jsonTxt.Write(retD.ToString(System.Globalization.CultureInfo.InvariantCulture));
                return(retD);

            case Token.STRING_ADD:
            case Token.STRING:
                value = DecodeString(token, false);
                o.Tag = JsonTag.JSON_STRING;
                o.doubleOrString.pos    = (int)jsonUTF8.Length + 1;
                o.doubleOrString.length = value.Length;
                jsonTxt.Write('"');
                jsonTxt.Write(value);
                jsonTxt.Write('"');
                return(value);

            case Token.STRING_GET:
                value = GetString(input.ReadVarint32());
                o.Tag = JsonTag.JSON_STRING;
                o.doubleOrString.pos    = (int)jsonUTF8.Length + 1;
                o.doubleOrString.length = value.Length;
                jsonTxt.Write('"');
                jsonTxt.Write(value);
                jsonTxt.Write('"');
                return(value);

            case Token.BINARY:
                return(DecodeBinary());

            default:
                throw new PsonException("illegal token: 0x" + token.ToString("x2"));                         // should never happen
            }
        }
Esempio n. 4
0
        internal StringBuilder Convert2CSVvisual(Boolean horizontal, Boolean sortLast, String id)
        {
            if (null == JSON)
            {
                return(new StringBuilder("No JSON data !"));
            }
            VisualNode3         node = JSON.Node_Viewer;
            Stack <VisualNode3> s    = new Stack <VisualNode3>();

            s.Push(node);
            List <String> path  = new List <String>();
            StringBuilder text  = new StringBuilder();
            int           keyNo = 1;
            List <String> rows  = new List <String>();
            StringBuilder rowBd = null;

            //if (id.Length == 0) sortLast = false;
            if (sortLast)
            {
                rowBd = new StringBuilder();
            }
            while (s.Count > 0)
            {
                node = s.Pop();
                if (null != node.m_JsonNode.node)
                {
                    while (path.Count - s.Count > 1)
                    {
                        path.RemoveAt(0);
                        keyNo = 1;
                    }
                    if (node.m_JsonNode.HasKey)
                    {
                        path.Add(node.Key_Viewer);
                    }
                    else
                    {
                        if (sortLast)
                        {
                            path.Add("");           // path.Add($"No{keyNo++:00000000}");
                        }
                        else
                        {
                            path.Add($"No{keyNo++}");
                        }
                    }
                    if (null != node.m_JsonNode.next)
                    {
                        s.Push(node.Next_Viewer);
                    }
                    s.Push(node.Node_Viewer);
                }
                else if (null != node.m_JsonNode.next)
                {
                    int row = 0;
                    if (sortLast)
                    {
                        SortedList <String, String> lastChildrens = new SortedList <String, String>();
                        VisualNode3 keyNode = node;
                        while (null != keyNode)
                        {
                            if (JsonTag.JSON_ARRAY == keyNode.Tag_Viewer || JsonTag.JSON_OBJECT == keyNode.Tag_Viewer)
                            {
                                s.Push(keyNode);
                                if (null != keyNode.m_JsonNode.next)
                                {
                                    break;
                                }
                            }
                            else
                            {
                                if (id == keyNode.Key_Viewer)
                                {
                                    if (path.Count > 0)
                                    {
                                        path[path.Count - 1] = keyNode.Value_Viewer;
                                    }
                                    else
                                    {
                                        path.Add(keyNode.Value_Viewer);
                                    }
                                }
                                else if (sortLast)
                                {
                                    lastChildrens[keyNode.Key_Viewer] = keyNode.Value_Viewer;
                                }
                            }
                            keyNode = keyNode.Next_Viewer;
                        }
                        String key = "";
                        key = String.Join("\\", path.ToArray());
                        if (0 == row++ && horizontal)
                        {
                            rowBd.Append(key).Append('\\');
                        }
                        foreach (KeyValuePair <String, String> item in lastChildrens)
                        {
                            if (horizontal)
                            {
                                rowBd.Append('\t').Append(item.Key).Append('\t').Append(item.Value);
                            }
                            else
                            {
                                rowBd.Append(key).Append('\\').Append('\t').Append(item.Key).Append('\t').Append(item.Value).Append('\n');
                            }
                        }
                        if (horizontal)
                        {
                            rowBd.Append('\n');
                        }
                        rows.Add(rowBd.ToString());
                        rowBd.Length = 0;
                    }
                    else
                    {
                        JsonNode keyNode = node.m_JsonNode;
                        if (id.Length > 0)
                        {
                            while (null != keyNode)
                            {
                                if (id == keyNode.Key(JSONdata))
                                {
                                    if (path.Count > 0)
                                    {
                                        path[path.Count - 1] = keyNode.ToString(JSONdata);
                                    }
                                    else
                                    {
                                        path.Add(keyNode.ToString(JSONdata));
                                    }
                                    break;
                                }
                                keyNode = keyNode.next;
                            }
                        }
                        do
                        {
                            if (JsonTag.JSON_ARRAY == node.Tag_Viewer || JsonTag.JSON_OBJECT == node.Tag_Viewer)
                            {
                                s.Push(node);
                                if (null != node.m_JsonNode.next)
                                {
                                    break;
                                }
                            }
                            else
                            {
                                if (id.Length == 0 || id != node.Key_Viewer)
                                {
                                    if (0 == row++ || !horizontal)
                                    {
                                        text.Append(String.Join("\\", path.ToArray())).Append("\\\t");
                                    }
                                    else
                                    {
                                        if (horizontal)
                                        {
                                            text.Append('\t');
                                        }
                                        else
                                        {
                                            text.Append('\n');
                                        }
                                    }
                                    text.Append(node.Key_Viewer).Append('\t').Append(node.Value_Viewer);
                                    if (!horizontal)
                                    {
                                        text.Append('\n');
                                    }
                                }
                            }
                            node = node.Next_Viewer;
                        } while (null != node);
                        if (horizontal)
                        {
                            text.Append('\n');
                        }
                    }
                    if (path.Count > 0)
                    {
                        path.RemoveAt(path.Count - 1);
                    }
                }
            }
            if (sortLast)
            {
                rows.Sort();
                foreach (var item in rows)
                {
                    text.Append(item);
                }
            }
            return(text);
        }