/// <summary> /// Writes the JSON representation of the object. /// </summary> /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param> /// <param name="value">The value.</param> /// <param name="serializer">The calling serializer.</param> public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { Regex regex = (Regex)value; CborDataWriter cborWriter = writer as CborDataWriter; if (cborWriter == null) { throw ExceptionUtils.CreateJsonSerializationException(cborWriter as IJsonLineInfo, cborWriter.Path, $"{nameof(CborDataRegexConverter)} only supports writing a regex with {nameof(CborDataWriter)}.", null); } WriteCbor(cborWriter, regex); }
/// <summary> /// Writes the JSON representation of the object. /// </summary> /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param> /// <param name="value">The value.</param> /// <param name="serializer">The calling serializer.</param> public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { Regex regex = (Regex)value; BsonDataWriter bsonWriter = writer as BsonDataWriter; if (bsonWriter == null) { throw ExceptionUtils.CreateJsonSerializationException(bsonWriter as IJsonLineInfo, bsonWriter.Path, "BsonDataRegexConverter only supports writing a regex with BsonDataWriter.", null); } WriteBson(bsonWriter, regex); }
/// <summary> /// Reads the JSON representation of the object. /// </summary> /// <param name="reader">The <see cref="JsonReader"/> to read from.</param> /// <param name="objectType">Type of the object.</param> /// <param name="existingValue">The existing value of object being read.</param> /// <param name="serializer">The calling serializer.</param> /// <returns>The object value.</returns> public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { switch (reader.TokenType) { case JsonToken.StartObject: return(ReadRegexObject(reader, serializer)); case JsonToken.String: return(ReadRegexString(reader)); case JsonToken.Null: return(null); } throw ExceptionUtils.CreateJsonSerializationException(reader as IJsonLineInfo, reader.Path, "Unexpected token when reading Regex.", null); }
private Regex ReadRegexObject(JsonReader reader, JsonSerializer serializer) { string pattern = null; RegexOptions?options = null; while (reader.Read()) { switch (reader.TokenType) { case JsonToken.PropertyName: string propertyName = reader.Value.ToString(); if (!reader.Read()) { throw ExceptionUtils.CreateJsonSerializationException(reader as IJsonLineInfo, reader.Path, "Unexpected end when reading Regex.", null); } if (string.Equals(propertyName, PatternName, StringComparison.OrdinalIgnoreCase)) { pattern = (string)reader.Value; } else if (string.Equals(propertyName, OptionsName, StringComparison.OrdinalIgnoreCase)) { options = serializer.Deserialize <RegexOptions>(reader); } else { reader.Skip(); } break; case JsonToken.Comment: break; case JsonToken.EndObject: if (pattern == null) { throw ExceptionUtils.CreateJsonSerializationException(reader as IJsonLineInfo, reader.Path, "Error deserializing Regex. No pattern found.", null); } return(new Regex(pattern, options ?? RegexOptions.None)); } } throw ExceptionUtils.CreateJsonSerializationException(reader as IJsonLineInfo, reader.Path, "Unexpected end when reading Regex.", null); }