/// <summary> /// Serializes the specified object. /// </summary> /// <param name="emitter">The <see cref="IEmitter" /> where to serialize the object.</param> /// <param name="graph">The object to serialize.</param> /// <param name="type">The static type of the object to serialize.</param> /// <param name="contextSettings">The context settings.</param> public void Serialize(IEmitter emitter, object graph, Type type, SerializerContextSettings contextSettings = null) { if (emitter == null) { throw new ArgumentNullException("emitter"); } if (graph == null && type == null) { throw new ArgumentNullException("type"); } // Configure the emitter // TODO the current emitter is not enough configurable to format its output // This should be improved var defaultEmitter = emitter as Emitter; if (defaultEmitter != null) { defaultEmitter.ForceIndentLess = settings.IndentLess; } var context = new SerializerContext(this, contextSettings) { Emitter = emitter, Writer = CreateEmitter(emitter) }; // Serialize the document context.Writer.StreamStart(); context.Writer.DocumentStart(); context.WriteYaml(graph, type); context.Writer.DocumentEnd(); context.Writer.StreamEnd(); }
/// <summary> /// Serializes the specified object to a string. /// </summary> /// <param name="graph">The graph.</param> /// <param name="expectedType">The expected type.</param> /// <param name="contextSettings">The context settings.</param> /// <returns>A YAML string of the object.</returns> public string Serialize(object graph, Type expectedType, SerializerContextSettings contextSettings = null) { var stringWriter = new StringWriter(); Serialize(stringWriter, graph, expectedType, contextSettings); return(stringWriter.ToString()); }
/// <summary> /// Deserializes an object from the specified <see cref="Stream" /> with an expected specific type. /// </summary> /// <param name="stream">The stream.</param> /// <param name="expectedType">The expected type.</param> /// <param name="contextSettings">The context settings.</param> /// <param name="context">The context used to deserialize this object.</param> /// <returns>A deserialized object.</returns> /// <exception cref="System.ArgumentNullException">stream</exception> public object Deserialize(Stream stream, Type expectedType, SerializerContextSettings contextSettings, out SerializerContext context) { if (stream == null) { throw new ArgumentNullException("stream"); } return(Deserialize(new StreamReader(stream), expectedType, null, contextSettings, out context)); }
/// <summary> /// Initializes a new instance of the <see cref="SerializerContext"/> class. /// </summary> /// <param name="serializer">The serializer.</param> /// <param name="serializerContextSettings">The serializer context settings.</param> internal SerializerContext(Serializer serializer, SerializerContextSettings serializerContextSettings) { Serializer = serializer; settings = serializer.Settings; tagTypeRegistry = settings.AssemblyRegistry; ObjectFactory = settings.ObjectFactory; ObjectSerializerBackend = settings.ObjectSerializerBackend; Schema = Settings.Schema; ObjectSerializer = serializer.ObjectSerializer; typeDescriptorFactory = serializer.TypeDescriptorFactory; contextSettings = serializerContextSettings ?? SerializerContextSettings.Default; }
/// <summary> /// Serializes the specified object. /// </summary> /// <param name="stream">The stream.</param> /// <param name="graph">The object to serialize.</param> /// <param name="contextSettings">The context settings.</param> public void Serialize(Stream stream, object graph, Type expectedType, SerializerContextSettings contextSettings = null) { var writer = new StreamWriter(stream); try { Serialize(writer, graph, expectedType, contextSettings); } finally { try { writer.Flush(); } catch (Exception) { } } }
private static List<ParsingEvent> SerializeComponent(EntityComponent component) { // Wrap component in a EntityComponentCollection to properly handle errors var components = new Entity { component }.Components; // Serialize with Yaml layer var parsingEvents = new List<ParsingEvent>(); // We also want to serialize live component variables var serializerContextSettings = new SerializerContextSettings { MemberMask = DataMemberAttribute.DefaultMask | ScriptComponent.LiveScriptingMask }; YamlSerializer.Serialize(new ParsingEventListEmitter(parsingEvents), components, typeof(EntityComponentCollection), serializerContextSettings); return parsingEvents; }
protected override List<ParsingEvent> SerializeScript(Script script) { // Wrap script in a ScriptCollection to properly handle errors var scriptCollection = new ScriptCollection { script }; // Serialize with Yaml layer var parsingEvents = new List<ParsingEvent>(); // We also want to serialize live scripting variables var serializerContextSettings = new SerializerContextSettings { MemberMask = DataMemberAttribute.DefaultMask | Script.LiveScriptingMask }; YamlSerializer.Serialize(new ParsingEventListEmitter(parsingEvents), scriptCollection, typeof(ScriptCollection), serializerContextSettings); return parsingEvents; }
/// <summary> /// Deserializes an object from the specified <see cref="EventReader" /> with an expected specific type. /// </summary> /// <param name="reader">The reader.</param> /// <param name="expectedType">The expected type, maybe null.</param> /// <param name="existingObject">An existing object, may be null.</param> /// <param name="contextSettings">The context settings.</param> /// <param name="context">The context used to deserialize the object.</param> /// <returns>A deserialized object.</returns> /// <exception cref="System.ArgumentNullException">reader</exception> public object Deserialize(EventReader reader, Type expectedType, object existingObject, SerializerContextSettings contextSettings, out SerializerContext context) { if (reader == null) { throw new ArgumentNullException("reader"); } var hasStreamStart = reader.Allow <StreamStart>() != null; var hasDocumentStart = reader.Allow <DocumentStart>() != null; context = null; object result = null; if (!reader.Accept <DocumentEnd>() && !reader.Accept <StreamEnd>()) { context = new SerializerContext(this, contextSettings) { Reader = reader }; result = context.ReadYaml(existingObject, expectedType); } if (hasDocumentStart) { reader.Expect <DocumentEnd>(); } if (hasStreamStart) { reader.Expect <StreamEnd>(); } return(result); }
/// <summary> /// Deserializes an object from the specified <see cref="EventReader" /> with an expected specific type. /// </summary> /// <param name="reader">The reader.</param> /// <param name="expectedType">The expected type, maybe null.</param> /// <param name="existingObject">An existing object, may be null.</param> /// <param name="contextSettings">The context settings.</param> /// <returns>A deserialized object.</returns> /// <exception cref="System.ArgumentNullException">reader</exception> public object Deserialize(EventReader reader, Type expectedType, object existingObject = null, SerializerContextSettings contextSettings = null) { SerializerContext context; return(Deserialize(reader, expectedType, existingObject, contextSettings, out context)); }
/// <summary> /// Deserializes an object from the specified <see cref="TextReader" /> with an expected specific type. /// </summary> /// <param name="reader">The reader.</param> /// <param name="expectedType">The expected type.</param> /// <param name="existingObject">The object to deserialize into. If null (the default) then a new object will be created</param> /// <param name="contextSettings">The context settings.</param> /// <param name="context">The context used to deserialize this object.</param> /// <returns>A deserialized object.</returns> /// <exception cref="System.ArgumentNullException">reader</exception> public object Deserialize(TextReader reader, Type expectedType, object existingObject, SerializerContextSettings contextSettings, out SerializerContext context) { if (reader == null) { throw new ArgumentNullException("reader"); } return(Deserialize(new EventReader(new Parser(reader)), expectedType, existingObject, contextSettings, out context)); }
/// <summary> /// Serializes the specified object. /// </summary> /// <param name="writer">The <see cref="TextWriter" /> where to serialize the object.</param> /// <param name="graph">The object to serialize.</param> /// <param name="type">The static type of the object to serialize.</param> /// <param name="contextSettings">The context settings.</param> public void Serialize(TextWriter writer, object graph, Type type, SerializerContextSettings contextSettings = null) { Serialize(new Emitter(writer, Settings.PreferredIndent), graph, type, contextSettings); }
/// <summary> /// Serializes the specified object. /// </summary> /// <param name="writer">The <see cref="TextWriter" /> where to serialize the object.</param> /// <param name="graph">The object to serialize.</param> /// <param name="type">The static type of the object to serialize.</param> /// <param name="contextSettings">The context settings.</param> public void Serialize(TextWriter writer, object graph, Type type, SerializerContextSettings contextSettings = null) { Serialize(new Emitter(writer, Settings.PreferredIndent, emitKeyQuoted: Settings.EmitJsonComptible), graph, type, contextSettings); }