public override T Read(ref Utf8JsonReader reader, Type objectType, JsonSerializerOptions options) { reader.AssertJsonType(JsonTokenType.String); try { IBitcoinSerializable obj = null; var bytes = Encoders.Hex.DecodeData(reader.GetString()); if (!Network.Consensus.ConsensusFactory.TryCreateNew(objectType, out obj)) { if (objectType == typeof(PubKey)) { obj = new PubKey(bytes); } else { obj = (T)Activator.CreateInstance(objectType); } } obj.ReadWrite(bytes, Network); return((T)obj); } catch (EndOfStreamException) { } catch (FormatException) { } throw new JsonException("Invalid bitcoin object of type " + objectType.Name); }
public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { reader.AssertJsonType(JsonTokenType.Number); var result = NBitcoin.Utils.UnixTimeToDateTime(reader.GetUInt64()); return(result); }
public override KeyPath Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { if (reader.TokenType == JsonTokenType.Null) { return(null); } reader.AssertJsonType(JsonTokenType.String); if (KeyPath.TryParse(reader.GetString(), out var k)) { return(k); } throw new JsonException("Invalid key path"); }