Exemplo n.º 1
0
        public static void Serialize <T>(T objectToSerialize, IJsonWriter writer, SerializationContext context)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            if (objectToSerialize == null)
            {
                writer.WriteNull();
                writer.Flush();
                return;
            }

            writer.WriteValue(objectToSerialize, typeof(T));
            writer.Flush();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Writes the current Json object.
        /// </summary>
        /// <param name="reader">The Json reader providing the data.</param>
        /// <param name="jsonWriter">The Json writer writes data into memory stream.</param>
        private static void WriteCurrentJsonObject(IJsonReader reader, IJsonWriter jsonWriter)
        {
            Stack <JsonNodeType> nodeTypes = new Stack <JsonNodeType>();

            do
            {
                switch (reader.NodeType)
                {
                case JsonNodeType.PrimitiveValue:
                {
                    if (reader.Value != null)
                    {
                        jsonWriter.WritePrimitiveValue(reader.Value);
                    }
                    else
                    {
                        jsonWriter.WriteValue((string)null);
                    }
                }

                break;

                case JsonNodeType.Property:
                {
                    jsonWriter.WriteName(reader.Value.ToString());
                }

                break;

                case JsonNodeType.StartObject:
                {
                    nodeTypes.Push(reader.NodeType);
                    jsonWriter.StartObjectScope();
                }

                break;

                case JsonNodeType.StartArray:
                {
                    nodeTypes.Push(reader.NodeType);
                    jsonWriter.StartArrayScope();
                }

                break;

                case JsonNodeType.EndObject:
                {
                    Debug.Assert(nodeTypes.Peek() == JsonNodeType.StartObject);
                    nodeTypes.Pop();
                    jsonWriter.EndObjectScope();
                }

                break;

                case JsonNodeType.EndArray:
                {
                    Debug.Assert(nodeTypes.Peek() == JsonNodeType.StartArray);
                    nodeTypes.Pop();
                    jsonWriter.EndArrayScope();
                }

                break;

                default:
                {
                    throw new ODataException(String.Format(
                                                 CultureInfo.InvariantCulture,
                                                 "Unexpected reader.NodeType: {0}.",
                                                 reader.NodeType));
                }
                }

                reader.ReadNext(); // This can be EndOfInput, where nodeTypes should be empty.
            }while (nodeTypes.Count != 0);

            jsonWriter.Flush();
        }