다음 POCO가 주어지면 :
public class AggV2
{
[JsonProperty("T")]
public string Ticker { get; set; }
[JsonProperty("v")]
public decimal Volume { get; set; }
[JsonProperty("o")]
public decimal Open { get; set; }
[JsonProperty("c")]
public decimal Close { get; set; }
[JsonProperty("h")]
public decimal High { get; set; }
[JsonProperty("l")]
public decimal Low { get; set; }
[JsonProperty("t")]
[JsonConverter(typeof(UnixMillisecondsConverter))]
public DateTimeOffset Timestamp { get; set; }
}
타사 데이터 제공 업체가 동일한 시세, 즉 MsFT 및 MSFT에 대해 대소 문자를 혼합하여 보내는 것을 보았습니다. 이로 인해 MSSQL 키 제약 조건으로 인해 악몽이 발생하고 있습니다!
첫 번째 단계는 Ticker
위의 예 에서 속성 값이 항상 MSFT가 되도록 일반적인 경우로 필터링하는 것입니다.
Ticker
항상 대문자로 직렬화 되도록하려면 어떻게해야 합니까?
다음 변환기를 작성했습니다. 이것이 최상의 방법일까요? 아니면 바퀴를 다시 발명 했나요?
public class AggV2
{
[JsonProperty("T")]
[JsonConverter(typeof(UppercaseStringConverter))]
public string Ticker { get; set; }
[JsonProperty("v")]
public decimal Volume { get; set; }
[JsonProperty("o")]
public decimal Open { get; set; }
[JsonProperty("c")]
public decimal Close { get; set; }
[JsonProperty("h")]
public decimal High { get; set; }
[JsonProperty("l")]
public decimal Low { get; set; }
[JsonProperty("t")]
[JsonConverter(typeof(UnixMillisecondsConverter))]
public DateTimeOffset Timestamp { get; set; }
[JsonProperty("n")]
public int Samples { get; set; }
}
public class UppercaseStringConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(string);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (value is string str)
writer.WriteValue(str.ToUpper());
else
throw new JsonSerializationException("Expected date object value.");
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
bool nullable = Nullable.GetUnderlyingType(objectType) != null;
if (reader.TokenType == JsonToken.Null)
{
if (!nullable)
{
throw new JsonSerializationException($"Cannot convert null value to {objectType}.");
}
return null;
}
if (reader.TokenType == JsonToken.String)
return ((string)reader.Value).ToUpper();
else
throw new JsonSerializationException($"Unexpected token parsing date. Expected String, got {reader.TokenType}.");
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다