protected T ReadJson <T>(Newtonsoft.Json.JsonConverter converter, Newtonsoft.Json.JsonReader reader, bool mustBeNested = false) { if (mustBeNested) { reader.Read(); if (reader.TokenType != Newtonsoft.Json.JsonToken.StartObject) { Assert.Fail("Expected StartObject token."); } reader.Read(); if (reader.TokenType != Newtonsoft.Json.JsonToken.PropertyName) { Assert.Fail("Expected PropertyName token."); } } // note: Json.NET calls Read before calling a converter if (!reader.Read()) { Assert.Fail("Unexpected end of stream."); } var serializer = CreatedConfiguredNewtonsoftJsonSerializer(); var result = (T)converter.ReadJson(reader, typeof(T), null, serializer); if (mustBeNested) { reader.Read(); if (reader.TokenType != Newtonsoft.Json.JsonToken.EndObject) { Assert.Fail("Expected EndObject token."); } } return(result); }
protected T ReadJsonUsingNativeJsonReader <T>(Newtonsoft.Json.JsonConverter converter, string json, bool mustBeNested = false) { using (var stringReader = new StringReader(json)) using (var reader = new Newtonsoft.Json.JsonTextReader(stringReader)) { return(ReadJson <T>(converter, reader, mustBeNested)); } }
protected T ReadJsonUsingNativeBsonReader <T>(Newtonsoft.Json.JsonConverter converter, byte[] bson, bool mustBeNested = false) { using (var stream = new MemoryStream(bson)) using (var reader = new Newtonsoft.Json.Bson.BsonDataReader(stream)) { return(ReadJson <T>(converter, reader, mustBeNested)); } }
protected T ReadJsonUsingWrappedJsonReader <T>(Newtonsoft.Json.JsonConverter converter, string json, bool mustBeNested = false) { using (var wrappedReader = new JsonReader(json)) using (var reader = new BsonReaderAdapter(wrappedReader)) { return(ReadJson <T>(converter, reader, mustBeNested)); } }
protected string WriteJsonUsingNativeJsonWriter(Newtonsoft.Json.JsonConverter converter, object value) { using (var stringWriter = new StringWriter()) using (var writer = new Newtonsoft.Json.JsonTextWriter(stringWriter)) { WriteJson(converter, value, writer); return(stringWriter.ToString()); } }
public DextopDateTimeConverter() { isoConverter = new Newtonsoft.Json.Converters.IsoDateTimeConverter { DateTimeStyles = System.Globalization.DateTimeStyles.AssumeLocal }; jsConverter = new Newtonsoft.Json.Converters.JavaScriptDateTimeConverter(); }
protected byte[] WriteJsonUsingNativeBsonWriter(Newtonsoft.Json.JsonConverter converter, object value, bool mustBeNested = false) { using (var stream = new MemoryStream()) using (var writer = new Newtonsoft.Json.Bson.BsonDataWriter(stream)) { WriteJson(converter, value, writer, mustBeNested); return(stream.ToArray()); } }
protected string WriteJsonUsingWrappedJsonWriter(Newtonsoft.Json.JsonConverter converter, object value) { using (var stringWriter = new StringWriter()) using (var wrappedWriter = new JsonWriter(stringWriter)) using (var writer = new BsonWriterAdapter(wrappedWriter)) { WriteJson(converter, value, writer); return(stringWriter.ToString()); } }
protected T ReadJsonUsingWrappedBsonReader <T>(Newtonsoft.Json.JsonConverter converter, byte[] bson, bool mustBeNested = false, GuidRepresentation guidRepresentation = GuidRepresentation.CSharpLegacy) { var readerSettings = new BsonBinaryReaderSettings { GuidRepresentation = guidRepresentation }; using (var stream = new MemoryStream(bson)) using (var wrappedReader = new BsonBinaryReader(stream, readerSettings)) using (var reader = new BsonReaderAdapter(wrappedReader)) { return(ReadJson <T>(converter, reader, mustBeNested)); } }
protected byte[] WriteJsonUsingWrappedBsonWriter(Newtonsoft.Json.JsonConverter converter, object value, bool mustBeNested = false, GuidRepresentation guidRepresentation = GuidRepresentation.CSharpLegacy) { var wrappedWriterSettings = new BsonBinaryWriterSettings { GuidRepresentation = guidRepresentation }; using (var stream = new MemoryStream()) using (var wrappedWriter = new BsonBinaryWriter(stream, wrappedWriterSettings)) using (var writer = new BsonWriterAdapter(wrappedWriter)) { WriteJson(converter, value, writer, mustBeNested); return(stream.ToArray()); } }
protected void WriteJson(Newtonsoft.Json.JsonConverter converter, object value, Newtonsoft.Json.JsonWriter writer, bool mustBeNested = false) { if (mustBeNested) { writer.WriteStartObject(); writer.WritePropertyName("x"); } var serializer = CreatedConfiguredNewtonsoftJsonSerializer(); converter.WriteJson(writer, value, serializer); if (mustBeNested) { writer.WriteEndObject(); } }
public TimeSpanConverter(Newtonsoft.Json.JsonConverter dateConverter) { this.dateConverter = dateConverter; }
/// <summary> /// Saves an object to a json-file. /// </summary> public static bool SaveJsonFile(string filePath, object data, out string errorMessage, Newtonsoft.Json.JsonConverter converter = null) { if (string.IsNullOrEmpty(filePath)) { errorMessage = "File path is empty.\nCouldn't be saved."; return(false); } errorMessage = null; try { using (StreamWriter sw = File.CreateText(filePath)) { var ser = new Newtonsoft.Json.JsonSerializer(); if (converter != null) { ser.Converters.Add(converter); } ser.Serialize(sw, data); } return(true); } catch (SerializationException ex) { errorMessage = $"File\n{Path.GetFullPath(filePath)}\ncouldn't be saved.\nError message:\n\n" + ex.Message; } return(false); }
/// <summary> /// Loads a serialized object from a json-file. /// </summary> public static bool LoadJsonFile <T>(string filePath, out T data, out string errorMessage, Newtonsoft.Json.JsonConverter converter = null) where T : class { errorMessage = null; data = null; // load json-file of data try { using (StreamReader sr = File.OpenText(filePath)) { var ser = new Newtonsoft.Json.JsonSerializer(); if (converter != null) { ser.Converters.Add(converter); } data = (T)ser.Deserialize(sr, typeof(T)); if (data != null) { return(true); } errorMessage = $"File\n{Path.GetFullPath(filePath)}\n contains no readable data."; return(false); } } catch (Newtonsoft.Json.JsonReaderException ex) { errorMessage = $"File\n{Path.GetFullPath(filePath)}\ncouldn't be opened or read.\nError message:\n\n" + ex.Message; } catch (Newtonsoft.Json.JsonSerializationException ex) { errorMessage = $"File\n{Path.GetFullPath(filePath)}\ncouldn't be opened or read.\nError message:\n\n" + ex.Message; } return(false); }
/// <summary> /// Deserializzazione sicura di un oggetto JSON /// </summary> /// <param name="jObject">Oggetto JSON</param> /// <param name="jsonValueName">Nome della proprietà di cui restituisce il valore</param> /// <param name="settings">Impostazioni per la deserializzazione</param> /// <param name="Result">Valore della proprietà</param> /// <returns>Restituisce true se l'operazione ha successo, false altrimenti</returns> static public bool SafeTryGetJSONDeserialize <T>(JObject jObject, string jsonValueName, Newtonsoft.Json.JsonConverter settings, out T Result) { Result = default; if (jObject == null) { return(false); } if (!jObject.ContainsKey(jsonValueName)) { return(false); } try { Result = Newtonsoft.Json.JsonConvert.DeserializeObject <T>(jObject[jsonValueName].ToString(), settings); return(true); } catch (Exception) { return(false); } }
public void Reflect() { var converters = new Newtonsoft.Json.JsonConverter[] { new NewtonsoftConverters.TypeConverter(), new Newtonsoft.Json.Converters.StringEnumConverter { }, new NewtonsoftConverters.MemberInfoConverter(), }; var sb = new StringBuilder(); foreach (var t in new[] { typeof(System.Int32), typeof(System.Boolean), typeof(System.Byte), typeof(System.Char), typeof(System.DateTime), typeof(System.Decimal), typeof(System.Double), typeof(System.Int16), typeof(System.Int64), typeof(System.SByte), typeof(System.Single), typeof(System.String), typeof(System.UInt16), typeof(System.UInt32), typeof(System.UInt64), typeof(System.Int32?), typeof(System.Boolean?), typeof(System.Byte?), typeof(System.Char?), typeof(System.DateTime?), typeof(System.Decimal?), typeof(System.Double?), typeof(System.Int16?), typeof(System.Int64?), typeof(System.SByte?), typeof(System.Single?), typeof(System.UInt16?), typeof(System.UInt32?), typeof(System.UInt64?), typeof(System.Guid), typeof(System.Guid?), typeof(IEnumerable <int>), typeof(string[]), typeof(IReadOnlyList <Guid>), GetType(new { i = 1 }), GetType(new[] { new { s = "" } }.ToList()), typeof(Dictionary <string, string>), typeof(IReadOnlyDictionary <Guid, System.DayOfWeek>), typeof(KeyValuePair <int, int>), typeof(KeyValuePair <int, int>?), typeof(DateTimeOffset), typeof(DateTimeOffset?), typeof(System.Net.IPAddress), typeof(ExplicitDataMemberOrder), typeof(DataMemberSomeOrder), typeof(NoDataMember), typeof(Marked), typeof(Dictionary <string, byte[]>), }) { var schema = Cameronism.Json.Schema.Reflect(t); var json = Newtonsoft.Json.JsonConvert.SerializeObject(schema, Newtonsoft.Json.Formatting.Indented, converters); sb.AppendLine(); sb.AppendLine(); sb.AppendLine("# " + HumanName(t)); sb.AppendLine(json); } ApprovalTests.Approvals.Verify(sb.ToString()); }