public WireSerializerAdapter() { var options = new SerializerOptions(versionTolerance: false, preserveObjectReferences: true); _serializer = new Serializer(options); }
/// <summary> /// Serialize an object to a byte array /// </summary> /// <param name="obj">The object to serialize</param> /// <param name="options">Serialization options</param> /// <param name="ignoreProperties">A list expressions that define properties to ignore</param> /// <returns></returns> public byte[] Serialize <T>(T obj, SerializerOptions options, params Expression <Func <T, object> >[] ignoreProperties) { return(Serialize <T>(obj, options, ConvertToPropertyNameList(ignoreProperties))); }
/// <summary> /// Serialize the object as XML /// </summary> /// <param name="obj">Object to serialize</param> /// <returns>XML as string</returns> public string Serialize(object obj, SerializerOptions options) { var doc = new XDocument(); var t = obj.GetType(); var name = t.Name; var serializeAsAttribute = t.GetAttribute<SerializeAsAttribute>(); if (serializeAsAttribute != null) { name = serializeAsAttribute.TransformName(serializeAsAttribute.Name ?? name); } var root = new XElement(name.AsNamespaced(Namespace)); if (obj is IList) { var itemTypeName = ""; foreach (var item in (IList)obj) { var type = item.GetType(); var opts = type.GetAttribute<SerializeAsAttribute>(); if (opts != null) { itemTypeName = opts.TransformName(opts.Name ?? name); } if (itemTypeName == "") { itemTypeName = type.Name; } var instance = new XElement(itemTypeName); Map(instance, item); root.Add(instance); } } else Map(root, obj); if (RootElement.HasValue()) { var wrapper = new XElement(RootElement.AsNamespaced(Namespace), root); doc.Add(wrapper); } else { doc.Add(root); } return doc.ToString(); }
public HyperionExpressionFormatter(SerializerOptions options) : base(options, null, ExpressionFieldInfoComparer.Instance) { }
public async Task <TReturn> DeserializeAsync <TReturn>(string json, CancellationToken cancellationToken, SerializerOptions options = null) { await using var stream = new MemoryStream(); return(await JsonSerializer.DeserializeAsync <TReturn>(stream, ToJsonOptions(options), cancellationToken)); }
public VoltaicJsonMvcOptionsSetup(SerializerOptions serializerOptions) { _serializerOptions = serializerOptions; }
/// <summary> /// Read the parent object, and recursively process it's children /// </summary> /// <param name="reader"></param> /// <param name="typeSupport">The type of the root object</param> /// <param name="maxDepth">The max depth tree to process</param> /// <param name="options">The serialization options</param> /// <param name="objectTree">Tracks the tree that has been traversed</param> /// <param name="ignoreAttributes">Properties/Fields with these attributes will be ignored from processing</param> /// <param name="typeRegistry">A registry that contains custom type mappings</param> /// <returns></returns> internal static object Read(BinaryReader reader, ExtendedType typeSupport, uint maxDepth, SerializerOptions options, ICollection <object> ignoreAttributes, TypeRegistry typeRegistry = null, ICollection <string> ignorePropertiesOrPaths = null) { var currentDepth = 0; uint dataLength = 0; uint headerLength = 0; var dataReader = reader; // read in byte 0, the data settings var dataSettings = (SerializerDataSettings)reader.ReadByte(); if (dataSettings.BitwiseHasFlag(SerializerDataSettings.Compress)) { // decompress the stream dataReader = Decompress(reader); } var typeReader = new TypeReader(dataSettings, options, maxDepth, ignoreAttributes, typeRegistry, ignorePropertiesOrPaths); return(typeReader.ReadObject(dataReader, typeSupport, currentDepth, string.Empty, ref dataLength, ref headerLength)); }
public async Task <string> SerializeAsync(object objectToSerialize, CancellationToken cancellationToken, SerializerOptions options = null) { await using var stream = new MemoryStream(); await JsonSerializer.SerializeAsync(stream, objectToSerialize, objectToSerialize.GetType(), ToJsonOptions(options), cancellationToken); stream.Position = 0; using var reader = new StreamReader(stream); return(await reader.ReadToEndAsync()); }
public HyperionFormatter(SerializerOptions options) : base(options) { }
public EntryStringSerializer() { Options = SerializerOptions.IncludeSeverity | SerializerOptions.IncludeMessage; DateFormat = "HH:mm:ss"; }
/// <summary> /// Initializes a new instance of the <see cref="Serializer" /> class. /// </summary> /// <param name="obj">The object.</param> /// <param name="depth">The depth.</param> /// <param name="format">if set to <c>true</c> [format].</param> /// <param name="options">The options.</param> private Serializer(object obj, int depth, bool format, SerializerOptions options) { // Basic Type Handling (nulls, strings and bool) _result = ResolveBasicType(obj, depth); if (string.IsNullOrWhiteSpace(_result) == false) { return; } if (options.IncludeProperties != null && options.IncludeProperties.Length > 0) { _includeProperties.AddRange(options.IncludeProperties); } if (options.ExcludeProperties != null && options.ExcludeProperties.Length > 0) { _excludeProperties.AddRange(options.ExcludeProperties); } _format = format; _lastCommaSearch = FieldSeparatorChar + (_format ? Environment.NewLine : string.Empty); var targetType = obj.GetType(); // Number or DateTime _result = ResolveDateTimeOrNumber(obj, targetType); if (string.IsNullOrWhiteSpace(_result) == false) { return; } var target = obj; // At this point, we will need to construct the object with a StringBuilder. _builder = new StringBuilder(); // Handle circular references correctly and avoid them if (options.ParentReferences.Any(p => ReferenceEquals(p.Target, obj))) { _result = $"{{ \"$circref\": \"{Escape(obj.GetHashCode().ToStringInvariant())}\" }}"; return; } options.ParentReferences.Add(new WeakReference(obj)); // Dictionary Type Handling (IDictionary) _result = ResolveDictionary(obj, depth, options); if (string.IsNullOrWhiteSpace(_result) == false) { return; } // Enumerable Type Handling (IEnumerable) _result = ResolveEnumerable(obj, depth, options); if (string.IsNullOrWhiteSpace(_result) == false) { return; } // If we arrive here, then we convert the object into a // dictionary of property names and values and call the serialization // function again var objectDictionary = CreateDictionary(options.TypeSpecifier, options.IncludeNonPublic, targetType, target); // At this point we either have a dictionary with or without properties // If we have at least one property then we send it through the serialization method // If we don't have any properties we return empty object if (objectDictionary.Count == 0) { _result = EmptyObjectLiteral; return; } _result = Serialize(objectDictionary, depth, _format, options); }
private static string Serialize(object obj, int depth, bool format, SerializerOptions options) { return(new Serializer(obj, depth, format, options)._result); }
public HyperionSerializer(ExtendedActorSystem system): base(system) { var knownTypes = new[] { typeof(Msg) }; var serializerOptions = new SerializerOptions(knownTypes: knownTypes); _serializer = new Hyperion.Serializer(serializerOptions); }
protected PerfTestBase(SerializerOptions options = null) { this.options = options; new TestRunner(null).SetProcessPriority(concurrent: false); }
private IEnumerable <Triple> generateGraph(object item, Uri id, Dictionary <string, PropertyMetadata> propertyMetadataDictionary, Dictionary <Type, Uri> classUriTypeDictionary, SerializerOptions serializerOptions, Dictionary <Uri, HashSet <Uri> > serializedSubjects) { TripleCollection result = new TripleCollection(); NodeFactory nodeFactory = new NodeFactory(); List <Triple> triples = new List <Triple>(); Type type = item.GetType(); IUriNode subjectNode = nodeFactory.CreateUriNode(id); Uri subjectType = classUriTypeDictionary[type]; if (serializerOptions != SerializerOptions.ExcludeRdfType) { triples.Add(new Triple(subjectNode, nodeFactory.CreateUriNode(new Uri(RdfSpecsHelper.RdfType)), nodeFactory.CreateUriNode(subjectType))); } if (serializedSubjects.ContainsKey(subjectNode.Uri)) { serializedSubjects[subjectNode.Uri].Add(subjectType); } else { serializedSubjects.Add(subjectNode.Uri, new HashSet <Uri>() { subjectType }); } foreach (string propertyName in giveMePropertyNames(type)) { PropertyMetadata propertyMetadata = propertyMetadataDictionary[propertyName]; object propertyValue = type.GetProperty(propertyName).GetValue(item, null); if (propertyValue != null) { IEnumerable <object> propertyValues = null; if (isTypeEnumerable(propertyValue.GetType())) { propertyValues = (propertyValue as IEnumerable).Cast <object>(); } else { propertyValues = new object[] { propertyValue } }; foreach (object itemValue in propertyValues) { if (propertyMetadata.IsComplexType) { Uri childId = new Uri(itemValue.GetType().GetProperty(idPropertyName).GetValue(itemValue, null).ToString()); triples.Add(new Triple(subjectNode, nodeFactory.CreateUriNode(propertyMetadata.PredicateUri), nodeFactory.CreateUriNode(childId))); triples.AddRange(generateGraph(itemValue, childId, propertyMetadataDictionary, classUriTypeDictionary, serializerOptions, serializedSubjects)); } else { ILiteralNode valueNode = null; if (propertyMetadata.ObjectRangeUri != null) { if (propertyMetadata.ObjectRangeUri.ToString() == "http://www.w3.org/2001/XMLSchema#date") { DateTimeOffset dt = (DateTimeOffset)itemValue; dt = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(dt, "GMT Standard Time"); valueNode = dt.ToLiteralDate(nodeFactory); } else if (propertyMetadata.ObjectRangeUri.ToString() == "http://www.w3.org/2001/XMLSchema#string") { valueNode = nodeFactory.CreateLiteralNode(itemValue.ToString()); } else if (propertyMetadata.ObjectRangeUri.ToString() == "http://www.w3.org/2001/XMLSchema#dateTime") { valueNode = ((DateTimeOffset)itemValue).ToLiteral(nodeFactory, true); } else { valueNode = nodeFactory.CreateLiteralNode(itemValue.ToString(), propertyMetadata.ObjectRangeUri); } } else { valueNode = nodeFactory.CreateLiteralNode(itemValue.ToString()); } triples.Add(new Triple(subjectNode, nodeFactory.CreateUriNode(propertyMetadata.PredicateUri), valueNode)); } } } } return(triples.Distinct()); }
public string Serialize(object obj, SerializerOptions options = null) { return(JsonSerializer.Serialize(obj, ToJsonOptions(options))); }
internal static string Serialize(object obj, int depth, SerializerOptions options) { return(new Serializer(obj, depth, options)._result); }
public TReturn Deserialize <TReturn>(string obj, SerializerOptions options = null) { return(JsonSerializer.Deserialize <TReturn>(obj, ToJsonOptions(options))); }
/// <summary> /// Write the parent object, and recursively process it's children /// </summary> /// <param name="writer"></param> /// <param name="obj"></param> /// <param name="typeSupport"></param> /// <param name="maxDepth"></param> /// <param name="options">The serialization options</param> /// <param name="objectTree"></param> /// <param name="ignoreAttributes"></param> internal static TypeDescriptors Write(BinaryWriter writer, object obj, ExtendedType typeSupport, uint maxDepth, SerializerOptions options, ICollection <object> ignoreAttributes, out string diagnosticLog, ICollection <string> ignorePropertiesOrPaths = null) { var currentDepth = 0; diagnosticLog = string.Empty; TypeDescriptors typeDescriptors = null; if (options.BitwiseHasFlag(SerializerOptions.EmbedTypes)) { typeDescriptors = new TypeDescriptors(); } var dataSettings = SerializerDataSettings.None; if (typeDescriptors != null) { dataSettings |= SerializerDataSettings.TypeMap; } if (options.BitwiseHasFlag(SerializerOptions.Compact)) { dataSettings |= SerializerDataSettings.Compact; } if (options.BitwiseHasFlag(SerializerOptions.Compress)) { dataSettings |= SerializerDataSettings.Compress; } // write the serializer byte 0, data settings writer.Write((byte)dataSettings); var typeWriter = new TypeWriter(maxDepth, dataSettings, options, ignoreAttributes, typeDescriptors, ignorePropertiesOrPaths); typeWriter.WriteObject(writer, obj, typeSupport, currentDepth, string.Empty, 0); if (options.BitwiseHasFlag(SerializerOptions.WriteDiagnosticLog)) { diagnosticLog = typeWriter.GetDiagnosticLog(); } return(typeDescriptors); }
public SqlDatabaseContextInfoDynamicTypeProvider(SerializationMemberInfo memberInfo, TypeSerializerCache cache, SerializerOptions options) { }
public VoltaicFormDataModelBinder(SerializerOptions options) { _options = options; }
public string Serialize(object obj, SerializerOptions options) { var ns = new XmlSerializerNamespaces(); ns.Add(string.Empty, Namespace); var serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType()); var writer = new EncodingStringWriter(Encoding); serializer.Serialize(writer, obj, ns); return writer.ToString(); }
/// <summary> /// Initializes a new instance of the <see cref="Serializer"/> class. /// </summary> /// <param name="options">The options to use when serializing values.</param> public Serializer(SerializerOptions options) { this.Options = options; }
/// <summary> /// Serialize the object as JSON /// </summary> /// <param name="obj">Object to serialize</param> /// <returns>JSON as String</returns> public string Serialize(object obj, SerializerOptions options) { return SimpleJson.SerializeObject(obj, options); }
public WireCommonSerializer() { var options = new SerializerOptions(); _serializer = new Serializer(options); }