Exemplo n.º 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")*/);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Validates that the reader is positioned on the specified node type.
        /// </summary>
        /// <param name="jsonValue">The <see cref="IJsonValue"/> to read from.</param>
        /// <param name="expectedKind">The expected JSON value kind.</param>
        /// <param name="jsonPath">The JSON path for current JSON value.</param>
        private static void ValidateValueKind(this IJsonValue jsonValue, JsonValueKind expectedKind, IJsonPath jsonPath)
        {
            Debug.Assert(jsonValue != null, "jsonValue != null");
            Debug.Assert(jsonPath != null, "jsonPath != null");

            if (jsonValue.ValueKind != expectedKind)
            {
                throw new Exception(/*Strings.JsonReader_UnexpectedJsonValueKind(jsonValue.ValueKind, jsonPath, expectedKind)*/);
            }
        }