internal static void ODataValueToString(StringBuilder sb, ODataValue value)
        {
            if (value == null || value is ODataNullValue)
            {
                sb.Append("null");
            }

            ODataCollectionValue collectionValue = value as ODataCollectionValue;

            if (collectionValue != null)
            {
                ODataCollectionValueToString(sb, collectionValue);
            }

            ODataResourceValue resourceValue = value as ODataResourceValue;

            if (resourceValue != null)
            {
                ODataResourceValueToString(sb, resourceValue);
            }

            ODataPrimitiveValue primitiveValue = value as ODataPrimitiveValue;

            if (primitiveValue != null)
            {
                if (primitiveValue.FromODataValue() is string)
                {
                    sb.Append("\"" + JsonValueUtils.GetEscapedJsonString(value.FromODataValue()?.ToString()) + "\"");
                }
                else
                {
                    sb.Append(JsonValueUtils.GetEscapedJsonString(value.FromODataValue()?.ToString()));
                }
            }
        }
        /// <summary>
        /// Writes the ODataValue (primitive, collection or resource value) to the underlying json writer.
        /// </summary>
        /// <param name="jsonWriter">The <see cref="JsonWriter"/> to write to.</param>
        /// <param name="odataValue">value to write.</param>
        internal static void WriteODataValue(this IJsonWriter jsonWriter, ODataValue odataValue)
        {
            if (odataValue == null || odataValue is ODataNullValue)
            {
                jsonWriter.WriteValue((string)null);
                return;
            }

            object objectValue = odataValue.FromODataValue();

            if (EdmLibraryExtensions.IsPrimitiveType(objectValue.GetType()))
            {
                jsonWriter.WritePrimitiveValue(objectValue);
                return;
            }

            ODataResourceValue resourceValue = odataValue as ODataResourceValue;

            if (resourceValue != null)
            {
                jsonWriter.StartObjectScope();

                foreach (ODataProperty property in resourceValue.Properties)
                {
                    jsonWriter.WriteName(property.Name);
                    jsonWriter.WriteODataValue(property.ODataValue);
                }

                jsonWriter.EndObjectScope();
                return;
            }

            ODataCollectionValue collectionValue = odataValue as ODataCollectionValue;

            if (collectionValue != null)
            {
                jsonWriter.StartArrayScope();

                foreach (object item in collectionValue.Items)
                {
                    // Will not be able to accurately serialize complex objects unless they are ODataValues.
                    ODataValue collectionItem = item as ODataValue;
                    if (item != null)
                    {
                        jsonWriter.WriteODataValue(collectionItem);
                    }
                    else
                    {
                        throw new ODataException(ODataErrorStrings.ODataJsonWriter_UnsupportedValueInCollection);
                    }
                }

                jsonWriter.EndArrayScope();

                return;
            }

            throw new ODataException(
                      ODataErrorStrings.ODataJsonWriter_UnsupportedValueType(odataValue.GetType().FullName));
        }