Beispiel #1
0
        public static JsonArray asArray(IList <string> list)
        {
            if (list != null)
            {
                JsonElement jsonElement = null;

                try
                {
                    jsonElement = GsonMapper.toJsonTree(list);
                }
                catch (JsonIOException e)
                {
                    LOG.logJsonException(e);
                }

                if (jsonElement != null)
                {
                    return(getArray(jsonElement));
                }
                else
                {
                    return(createArray());
                }
            }
            else
            {
                return(createArray());
            }
        }
Beispiel #2
0
        public static JsonObject asObject(IDictionary <string, object> properties)
        {
            if (properties != null)
            {
                JsonObject jsonObject = null;

                try
                {
                    jsonObject = (JsonObject)GsonMapper.toJsonTree(properties);
                }
                catch (Exception e) when(e is JsonIOException || e is System.InvalidCastException)
                {
                    LOG.logJsonException(e);
                }

                if (jsonObject != null)
                {
                    return(jsonObject);
                }
                else
                {
                    return(createObject());
                }
            }
            else
            {
                return(createObject());
            }
        }
Beispiel #3
0
        public static JsonObject asObject(string jsonString)
        {
            JsonObject jsonObject = null;

            if (!string.ReferenceEquals(jsonString, null))
            {
                try
                {
                    jsonObject = GsonMapper.fromJson(jsonString, typeof(JsonObject));
                }
                catch (Exception e) when(e is JsonParseException || e is System.InvalidCastException)
                {
                    LOG.logJsonException(e);
                }
            }

            if (jsonObject != null)
            {
                return(jsonObject);
            }
            else
            {
                return(createObject());
            }
        }
Beispiel #4
0
        public static JsonObject asObject(sbyte[] byteArray)
        {
            string stringValue = null;

            if (byteArray != null)
            {
                stringValue = StringUtil.fromBytes(byteArray);
            }

            if (string.ReferenceEquals(stringValue, null))
            {
                return(createObject());
            }

            JsonObject jsonObject = null;

            try
            {
                jsonObject = GsonMapper.fromJson(stringValue, typeof(JsonObject));
            }
            catch (JsonParseException e)
            {
                LOG.logJsonException(e);
            }

            if (jsonObject != null)
            {
                return(jsonObject);
            }
            else
            {
                return(createObject());
            }
        }
Beispiel #5
0
        public static object asPrimitiveObject(JsonPrimitive jsonValue)
        {
            if (jsonValue == null)
            {
                return(null);
            }

            object rawObject = null;

            if (jsonValue.Number)
            {
                object numberValue = null;

                try
                {
                    numberValue = jsonValue.AsNumber;
                }
                catch (System.FormatException e)
                {
                    LOG.logJsonException(e);
                }

                if (numberValue != null && numberValue is LazilyParsedNumber)
                {
                    string numberString = numberValue.ToString();
                    if (!string.ReferenceEquals(numberString, null))
                    {
                        rawObject = parseNumber(numberString);
                    }
                }
                else
                {
                    rawObject = numberValue;
                }
            }
            else
            {     // string, boolean
                try
                {
                    rawObject = GsonMapper.fromJson(jsonValue, typeof(object));
                }
                catch (Exception e) when(e is JsonSyntaxException || e is JsonIOException)
                {
                    LOG.logJsonException(e);
                }
            }

            if (rawObject != null)
            {
                return(rawObject);
            }
            else
            {
                return(null);
            }
        }
Beispiel #6
0
        public static void addFieldRawValue(JsonObject jsonObject, string memberName, object rawValue)
        {
            if (rawValue != null)
            {
                JsonElement jsonNode = null;

                try
                {
                    jsonNode = GsonMapper.toJsonTree(rawValue);
                }
                catch (JsonIOException e)
                {
                    LOG.logJsonException(e);
                }

                if (jsonNode != null)
                {
                    jsonObject.add(memberName, jsonNode);
                }
            }
        }
Beispiel #7
0
        public static sbyte[] asBytes(JsonElement jsonObject)
        {
            string jsonString = null;

            if (jsonObject != null)
            {
                try
                {
                    jsonString = GsonMapper.toJson(jsonObject);
                }
                catch (JsonIOException e)
                {
                    LOG.logJsonException(e);
                }
            }

            if (string.ReferenceEquals(jsonString, null))
            {
                jsonString = "";
            }

            return(StringUtil.toByteArray(jsonString));
        }