示例#1
0
        public JSONField(IEnumerable <JSONField> val, string identifier = null)
        {
            Identifier = identifier;

            Type      = FieldType.CONTAINER;
            Container = JSONContainer.NewArray(val);
        }
示例#2
0
        internal static bool TryParseObjectOrArray(string str, out JSONContainer obj, ref int index, out string errormessage, Stack <JSONContainer> containerStack)
        {
            obj          = null;
            errormessage = string.Empty;

            for (; index < str.Length; index++)
            {
                char current = str[index];
                if (current == '[')
                {
                    index++;
                    return(TryParseArray(str, out obj, ref index, out errormessage, containerStack));
                }
                else if (current == '{')
                {
                    index++;
                    return(TryParseObject(str, out obj, ref index, out errormessage, containerStack));
                }
                else if (JSONHelper.IsWhiteSpace(current))
                {
                    continue;
                }
                else
                {
                    errormessage = JSONHelper.GetErrorMessageString(str, index, JSONHelper.StructureType.Container, "Expected object ('{') or array ('[') scope start", containerStack);
                    return(false);
                }
            }
            errormessage = "Unexpected ending of input string";
            return(false);
        }
示例#3
0
 /// <summary>
 /// Attempts to retrieve a field as a JSONContainer object value
 /// </summary>
 /// <param name="identifier">Key/Name/Identifier identifying the field to be retrieved</param>
 /// <param name="val">JSONContainer object value stored in the field</param>
 /// <returns>True, if a matching field was obtained. False, otherwise</returns>
 public bool TryGetObjectField(string identifier, out JSONContainer val)
 {
     if (TryGetField(identifier, out JSONField field))
     {
         val = field.Container;
         return(field.IsObject);
     }
     val = default;
     return(false);
 }
示例#4
0
        public JSONField(JSONContainer val, string identifier = null)
        {
            Identifier = identifier;

            if (val == null)
            {
                Type = FieldType.NULL;
            }
            else
            {
                Type      = FieldType.CONTAINER;
                Container = val;
            }
        }
示例#5
0
        /// <summary>
        /// Attempts to parse a JSONContainer from a json formatted string
        /// </summary>
        /// <param name="str">As JSON formatted input string</param>
        /// <param name="result">The parsed JSONContainer result</param>
        /// <param name="errormessage">A string containing a hint in case parsing fails</param>
        /// <returns>True, if parsing is successful. False, otherwise</returns>
        public static bool TryParse(string str, out JSONContainer result, out string errormessage)
        {
            str = JSONHelper.TrimStringForParsing(str);

            if (!string.IsNullOrEmpty(str))
            {
                int index = 0;
                return(TryParseObjectOrArray(str, out result, ref index, out errormessage, new Stack <JSONContainer>()));
            }
            else
            {
                result       = null;
                errormessage = "Parsing String cannot be null or empty!";
                return(false);
            }
        }
示例#6
0
        private static void AnyContainerTest()
        {
            bool kill = false;

            while (!kill)
            {
                Console.WriteLine("Paste in Test JSON. Type \"go\" in a single line to start parsing. Type \"exit\" to quit program");
                string containerJSON = string.Empty;
                while (true)
                {
                    string line = Console.ReadLine();
                    if (line == "go")
                    {
                        break;
                    }
                    else if (line == "exit")
                    {
                        kill = true;
                        break;
                    }
                    else
                    {
                        containerJSON += line + '\n';
                    }
                }
                if (containerJSON == "exit")
                {
                    break;
                }
                if (!string.IsNullOrEmpty(containerJSON))
                {
                    if (JSONContainer.TryParse(containerJSON, out JSONContainer result, out string errormessage))
                    {
                        Console.WriteLine("\nParseSuccess!\n\n" + result.Build(true));
                        result.TryGetField("BotAdminIDs", out JSONContainer test);
                    }
                    else
                    {
                        Console.WriteLine(errormessage);
                    }
                }
                Console.WriteLine();
            }
示例#7
0
        private static bool TryParseObject(string str, out JSONContainer obj, ref int index, out string errormessage, Stack <JSONContainer> containerStack)
        {
            obj = new JSONContainer();
            containerStack.Push(obj);
            errormessage = string.Empty;
            int    fieldIdentifierStart = index;
            string fieldIdentifier      = null;

            ObjectParseStep parseStep = ObjectParseStep.SeekingFieldIdentifierOrScopeEnd;

            for (; index < str.Length; index++)
            {
                char current = str[index];
                switch (parseStep)
                {
                case ObjectParseStep.SeekingFieldIdentifierOrScopeEnd:
                {
                    if (current == '\"')
                    {
                        fieldIdentifierStart = index + 1;
                        parseStep            = ObjectParseStep.ParsingFieldIdentifier;
                    }
                    else if (current == '}')
                    {
                        index++;
                        containerStack.Pop();
                        return(true);
                    }
                    else if (JSONHelper.IsWhiteSpace(current))
                    {
                        continue;
                    }
                    else
                    {
                        errormessage = JSONHelper.GetErrorMessageString(str, index, JSONHelper.StructureType.Object, "Expected field identifier start '\"' or object scope end '}'", containerStack);
                        return(false);
                    }
                }
                break;

                case ObjectParseStep.ParsingFieldIdentifier:
                {
                    if (current == '\"')
                    {
                        if (index - 1 > fieldIdentifierStart)
                        {
                            fieldIdentifier = str.Substring(fieldIdentifierStart, index - fieldIdentifierStart);
                            parseStep       = ObjectParseStep.SeekingColon;
                        }
                        else if (index - 1 == fieldIdentifierStart)
                        {
                            fieldIdentifier = str[fieldIdentifierStart].ToString();
                            parseStep       = ObjectParseStep.SeekingColon;
                        }
                        else
                        {
                            errormessage = JSONHelper.GetErrorMessageString(str, index, JSONHelper.StructureType.Object, "Encountered empty (length = 0) field identifier!", containerStack);
                            return(false);
                        }
                    }
                    else
                    {
                        continue;
                    }
                }
                break;

                case ObjectParseStep.SeekingColon:
                {
                    if (current == ':')
                    {
                        parseStep = ObjectParseStep.SeekingValue;
                    }
                    else if (JSONHelper.IsWhiteSpace(current))
                    {
                        continue;
                    }
                    else
                    {
                        errormessage = JSONHelper.GetErrorMessageString(str, index, JSONHelper.StructureType.Object, "Expected colon separating field identifier and field value!", containerStack);
                        return(false);
                    }
                }
                break;

                case ObjectParseStep.SeekingValue:
                {
                    if (JSONField.TryParseProgressive(str, fieldIdentifier, out JSONField field, ref index, out errormessage, containerStack))
                    {
                        if (!obj.TryAddField(field))
                        {
                            errormessage = JSONHelper.GetErrorMessageString(str, index, JSONHelper.StructureType.Object, "Duplicate field identifier \"{fieldIdentifier}\"", containerStack);
                            return(false);
                        }
                        if (index < str.Length)
                        {
                            if (str[index] == '}')
                            {
                                index++;
                                containerStack.Pop();
                                return(true);
                            }
                        }
                        else
                        {
                            errormessage = "Unexpected ending of input string";
                            return(false);
                        }
                        parseStep = ObjectParseStep.SeekingFieldIdentifierOrScopeEnd;
                    }
示例#8
0
 /// <summary>
 /// Adds a JSONContainer value to array
 /// </summary>
 /// <param name="val">JSONContainer value to add to the array</param>
 public void Add(JSONContainer val)
 {
     Add(new JSONField(val));
 }
示例#9
0
 /// <summary>
 /// Attempts to add a field containing a JSONContainer value
 /// </summary>
 /// <param name="identifier">Key/Name/Identifier identifying the field to be added</param>
 /// <param name="val">JSONContainer value to be stored in the new field</param>
 /// <returns>True, if field was successfully added. False, otherwise</returns>
 public bool TryAddField(string identifier, JSONContainer val)
 {
     return(TryAddField(new JSONField(val, identifier)));
 }
示例#10
0
        internal static bool TryParseProgressive(string str, string fieldIdentifier, out JSONField field, ref int index, out string errormessage, Stack <JSONContainer> containerStack)
        {
            field        = null;
            errormessage = string.Empty;
            int  fieldValueStart = index;
            bool valueIsInteger  = true;

            FieldParseStep parseStep = FieldParseStep.Seeking;

            for (; index < str.Length; index++)
            {
                char current = str[index];
                switch (parseStep)
                {
                case FieldParseStep.Seeking:
                {
                    if (!JSONHelper.IsWhiteSpace(current))
                    {
                        switch (current)
                        {
                        case '[':
                        case '{':
                        {
                            if (!JSONContainer.TryParseObjectOrArray(str, out JSONContainer fieldObj, ref index, out errormessage, containerStack))
                            {
                                return(false);
                            }
                            else
                            {
                                field = new JSONField(fieldObj, fieldIdentifier);
                                return(true);
                            }
                        }

                        case '0':
                        case '1':
                        case '2':
                        case '3':
                        case '4':
                        case '5':
                        case '6':
                        case '7':
                        case '8':
                        case '9':
                        case '-':
                        case '.':
                        {
                            fieldValueStart = index;
                            parseStep       = FieldParseStep.ParseNumber;
                        }
                        break;

                        case '\"':
                        {
                            fieldValueStart = index + 1;
                            parseStep       = FieldParseStep.ParseString;
                        }
                        break;

                        case 't':
                        case 'f':
                        case 'n':
                        {
                            string valueString;
                            if (str.Length - index >= 4)
                            {
                                valueString = str.Substring(index, 4);
                                if (valueString == JSONHelper.TRUE)
                                {
                                    field  = new JSONField(true, fieldIdentifier);
                                    index += 4;
                                    return(true);
                                }
                                else if (valueString == JSONHelper.NULL)
                                {
                                    field = new JSONField
                                    {
                                        Identifier = fieldIdentifier
                                    };
                                    index += 4;
                                    return(true);
                                }
                            }
                            if (str.Length - index >= 5)
                            {
                                if (str.Substring(index, 5) == JSONHelper.FALSE)
                                {
                                    field  = new JSONField(false, fieldIdentifier);
                                    index += 5;
                                    return(true);
                                }
                            }
                            errormessage = JSONHelper.GetErrorMessageString(str, index, JSONHelper.StructureType.Field, string.Format(UNEXPECTEDFIELDSTARTERRORFORMAT, current), containerStack);
                            return(false);
                        }

                        default:
                        {
                            errormessage = JSONHelper.GetErrorMessageString(str, index, JSONHelper.StructureType.Field, string.Format(UNEXPECTEDFIELDSTARTERRORFORMAT, current), containerStack);
                            return(false);
                        }
                        }
                    }
                }