You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ValueTuple is not Supported in Aspnet Core 3.1 with default Json. #1519
ValueTuples are currently not supported by System.Text.Json since they require field support and System.Text.Json only supports public properties currently.
Here's a potential workaround in the mean time using a custom converter:
publicclassValueTupleFactory:JsonConverterFactory{publicoverrideboolCanConvert(TypetypeToConvert){TypeiTuple=typeToConvert.GetInterface("System.Runtime.CompilerServices.ITuple");returniTuple!=null;}publicoverrideJsonConverterCreateConverter(TypetypeToConvert,JsonSerializerOptionsoptions){Type[]genericArguments=typeToConvert.GetGenericArguments();TypeconverterType=genericArguments.Lengthswitch{1=>typeof(ValueTupleConverter<>).MakeGenericType(genericArguments),2=>typeof(ValueTupleConverter<,>).MakeGenericType(genericArguments),3=>typeof(ValueTupleConverter<,,>).MakeGenericType(genericArguments),// And add other cases as needed
_ =>thrownewNotSupportedException(),};return(JsonConverter)Activator.CreateInstance(converterType);}}publicclassValueTupleConverter<T1>:JsonConverter<ValueTuple<T1>>{publicoverrideValueTuple<T1>Read(refUtf8JsonReaderreader,TypetypeToConvert,JsonSerializerOptionsoptions){ValueTuple<T1>result=default;if(!reader.Read()){thrownewJsonException();}while(reader.TokenType!=JsonTokenType.EndObject){if(reader.ValueTextEquals("Item1")&&reader.Read()){result.Item1=JsonSerializer.Deserialize<T1>(refreader,options);}else{thrownewJsonException();}reader.Read();}returnresult;}publicoverridevoidWrite(Utf8JsonWriterwriter,ValueTuple<T1>value,JsonSerializerOptionsoptions){writer.WriteStartObject();writer.WritePropertyName("Item1");JsonSerializer.Serialize<T1>(writer,value.Item1,options);writer.WriteEndObject();}}publicclassValueTupleConverter<T1,T2>:JsonConverter<ValueTuple<T1,T2>>{publicoverride(T1,T2)Read(refUtf8JsonReaderreader,TypetypeToConvert,JsonSerializerOptionsoptions){(T1,T2)result=default;if(!reader.Read()){thrownewJsonException();}while(reader.TokenType!=JsonTokenType.EndObject){if(reader.ValueTextEquals("Item1")&&reader.Read()){result.Item1=JsonSerializer.Deserialize<T1>(refreader,options);}elseif(reader.ValueTextEquals("Item2")&&reader.Read()){result.Item2=JsonSerializer.Deserialize<T2>(refreader,options);}else{thrownewJsonException();}reader.Read();}returnresult;}publicoverridevoidWrite(Utf8JsonWriterwriter,(T1,T2)value,JsonSerializerOptionsoptions){writer.WriteStartObject();writer.WritePropertyName("Item1");JsonSerializer.Serialize<T1>(writer,value.Item1,options);writer.WritePropertyName("Item2");JsonSerializer.Serialize<T2>(writer,value.Item2,options);writer.WriteEndObject();}}publicclassValueTupleConverter<T1,T2,T3>:JsonConverter<ValueTuple<T1,T2,T3>>{publicoverride(T1,T2,T3)Read(refUtf8JsonReaderreader,TypetypeToConvert,JsonSerializerOptionsoptions){(T1,T2,T3)result=default;if(!reader.Read()){thrownewJsonException();}while(reader.TokenType!=JsonTokenType.EndObject){if(reader.ValueTextEquals("Item1")&&reader.Read()){result.Item1=JsonSerializer.Deserialize<T1>(refreader,options);}elseif(reader.ValueTextEquals("Item2")&&reader.Read()){result.Item2=JsonSerializer.Deserialize<T2>(refreader,options);}elseif(reader.ValueTextEquals("Item3")&&reader.Read()){result.Item3=JsonSerializer.Deserialize<T3>(refreader,options);}else{thrownewJsonException();}reader.Read();}returnresult;}publicoverridevoidWrite(Utf8JsonWriterwriter,(T1,T2,T3)value,JsonSerializerOptionsoptions){writer.WriteStartObject();writer.WritePropertyName("Item1");JsonSerializer.Serialize<T1>(writer,value.Item1,options);writer.WritePropertyName("Item2");JsonSerializer.Serialize<T2>(writer,value.Item2,options);writer.WritePropertyName("Item3");JsonSerializer.Serialize<T3>(writer,value.Item3,options);writer.WriteEndObject();}}publicclassWeatherForecastValueTuple{publicValueTuple<string>Temp{get;set;}public(intx,inty)Location{get;set;}public(inta,Pocob,ValueTuple<string>c)Thruple{get;set;}}publicclassPoco{publicintAlpha{get;set;}public(List<int>,string)Beta{get;set;}}privatestaticvoidSupportValueTuple(){varobjectWithValueTuple=newWeatherForecastValueTuple{Location=(3,7),Temp=newValueTuple<string>("FOO"),Thruple=(8,newPoco{Alpha=9,Beta=(newList<int>(){-1,0,-1},"a list")},newValueTuple<string>("Bar"))};varoptions=newJsonSerializerOptions();options.Converters.Add(newValueTupleFactory());stringjsonString=JsonSerializer.Serialize(objectWithValueTuple,options);Console.WriteLine(jsonString);WeatherForecastValueTupleroundTrip=JsonSerializer.Deserialize<WeatherForecastValueTuple>(jsonString,options);Assert.Equal((3,7),roundTrip.Location);Assert.Equal(newValueTuple<string>("FOO"),roundTrip.Temp);}
Activity
rynowak commentedon Jan 9, 2020
/cc @ahsonkhan - feedback about JSON.
Gnbrkm41 commentedon Jan 9, 2020
Duplicate of #876?
ahsonkhan commentedon Jan 9, 2020
This is a duplicate of #876.
ValueTuplesare currently not supported bySystem.Text.Jsonsince they require field support andSystem.Text.Jsononly supports public properties currently.Other similar issues:
https://github.com/dotnet/corefx/issues/41420
https://github.com/dotnet/corefx/issues/36240
#552
Here's a potential workaround in the mean time using a custom converter:
Within Aspnet you can register it as follows: