Пример #1
0
        /// <summary>
        /// Read <see cref="IJsonValue"/> to a string, the input JsonValue should be a string,
        /// otherwise, throw unexpected value kind exception.
        /// </summary>
        /// <param name="jsonValue">The <see cref="IJsonValue"/> to parse from.</param>
        /// <param name="jsonPath">The JSON path for current JSON node which owns this value.</param>
        /// <returns>The string.</returns>
        public static string ReadPrimitiveString(this IJsonValue jsonValue, IJsonPath jsonPath)
        {
            //    EdmUtil.CheckArgumentNull(jsonValue, "jsonValue");
            //   EdmUtil.CheckArgumentNull(jsonPath, "jsonPath");

            jsonValue.ValidateValueKind(JsonValueKind.Primitive, jsonPath);

            JsonPrimitiveValue primitiveValue = (JsonPrimitiveValue)jsonValue;

            if (primitiveValue.Value == null)
            {
                return(null);
            }

            if (primitiveValue.Value.GetType() == typeof(string))
            {
                return((string)primitiveValue.Value);
            }

            throw new Exception(/*Strings.JsonReader_CannotReadValueAsType(primitiveValue.Value, jsonPath.Path, "String")*/);
        }
Пример #2
0
        /// <summary>
        /// Read <see cref="IJsonValue"/> to a string.
        /// </summary>
        /// <param name="jsonValue">The <see cref="IJsonValue"/> to parse from.</param>
        /// <returns>The string.</returns>
        public static string ReadAsJsonString(this IJsonValue jsonValue)
        {
            if (jsonValue == null)
            {
                throw new ArgumentNullException("jsonValue");
            }

            // TODO: convert to use IJsonWriter
            if (jsonValue.ValueKind == JsonValueKind.Array)
            {
                bool           first      = true;
                JsonArrayValue arrayValue = (JsonArrayValue)jsonValue;
                StringBuilder  sb         = new StringBuilder("[");
                foreach (var item in arrayValue)
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        sb.Append(",");
                    }

                    sb.Append(item.ReadAsJsonString());
                }

                sb.Append("]");

                return(sb.ToString());
            }
            else if (jsonValue.ValueKind == JsonValueKind.Object)
            {
                JsonObjectValue objectValue = (JsonObjectValue)jsonValue;
                StringBuilder   sb          = new StringBuilder("{");
                bool            first       = true;
                foreach (var property in objectValue)
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        sb.Append(",");
                    }

                    sb.Append("\"" + property.Key + "\":");
                    sb.Append(property.Value.ReadAsJsonString());
                }

                sb.Append("}");

                return(sb.ToString());
            }
            else
            {
                JsonPrimitiveValue primitiveValue = (JsonPrimitiveValue)jsonValue;
                if (primitiveValue.Value == null)
                {
                    return("null");
                }

                Type type = primitiveValue.Value.GetType();
                if (type == typeof(bool))
                {
                    bool boolValue = (bool)primitiveValue.Value;
                    if (boolValue)
                    {
                        return("true");
                    }

                    return("false");
                }

                if (type == typeof(int))
                {
                    int intValue = (int)primitiveValue.Value;
                    return(intValue.ToString(CultureInfo.InvariantCulture));
                }

                if (type == typeof(decimal))
                {
                    decimal decimalValue = (decimal)primitiveValue.Value;
                    return(decimalValue.ToString(CultureInfo.InvariantCulture));
                }

                if (type == typeof(double))
                {
                    double doubleValue = (double)primitiveValue.Value;
                    return(doubleValue.ToString(CultureInfo.InvariantCulture));
                }

                return("\"" + primitiveValue.Value.ToString() + "\"");
            }
        }