Rišo
Duplicate enum values serialization problems
Yes, that's exactly what I was considering. However, I still would like to hear your opinions on the matter.
Also I understand that this is a bit of an edge case, but wouldn't opening an issue on github be an appropriate next step?
Also I understand that this is a bit of an edge case, but wouldn't opening an issue on github be an appropriate next step?
9 replies
Duplicate enum values serialization problems
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
{
// If strings are allowed, attempt to write it out as a string value
if ((_converterOptions & EnumConverterOptions.AllowStrings) != 0)
{
ulong key = ConvertToUInt64(value);
if (_nameCacheForWriting.TryGetValue(key, out JsonEncodedText formatted))
{
writer.WriteStringValue(formatted);
return;
}
string original = value.ToString();
if (IsValidIdentifier(original))
{
// We are dealing with a combination of flag constants since
// all constant values were cached during warm-up.
Debug.Assert(original.Contains(ValueSeparator));
original = FormatJsonName(original, _namingPolicy);
if (_nameCacheForWriting.Count < NameCacheSizeSoftLimit)
{
formatted = JsonEncodedText.Encode(original, options.Encoder);
writer.WriteStringValue(formatted);
_nameCacheForWriting.TryAdd(key, formatted);
}
else
{
// We also do not create a JsonEncodedText instance here because passing the string
// directly to the writer is cheaper than creating one and not caching it for reuse.
writer.WriteStringValue(original);
}
return;
}
}
//The rest of the code
...
}
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
{
// If strings are allowed, attempt to write it out as a string value
if ((_converterOptions & EnumConverterOptions.AllowStrings) != 0)
{
ulong key = ConvertToUInt64(value);
if (_nameCacheForWriting.TryGetValue(key, out JsonEncodedText formatted))
{
writer.WriteStringValue(formatted);
return;
}
string original = value.ToString();
if (IsValidIdentifier(original))
{
// We are dealing with a combination of flag constants since
// all constant values were cached during warm-up.
Debug.Assert(original.Contains(ValueSeparator));
original = FormatJsonName(original, _namingPolicy);
if (_nameCacheForWriting.Count < NameCacheSizeSoftLimit)
{
formatted = JsonEncodedText.Encode(original, options.Encoder);
writer.WriteStringValue(formatted);
_nameCacheForWriting.TryAdd(key, formatted);
}
else
{
// We also do not create a JsonEncodedText instance here because passing the string
// directly to the writer is cheaper than creating one and not caching it for reuse.
writer.WriteStringValue(original);
}
return;
}
}
//The rest of the code
...
}
9 replies
Duplicate enum values serialization problems
Mentioned methods:
public EnumConverter(EnumConverterOptions converterOptions, JsonNamingPolicy? namingPolicy, JsonSerializerOptions serializerOptions)
{
...
#if NET
string[] names = Enum.GetNames<T>();
T[] values = Enum.GetValues<T>();
#else
string[] names = Enum.GetNames(Type);
Array values = Enum.GetValues(Type);
#endif
Debug.Assert(names.Length == values.Length);
JavaScriptEncoder? encoder = serializerOptions.Encoder;
for (int i = 0; i < names.Length; i++)
{
#if NET
T value = values[i];
#else
T value = (T)values.GetValue(i)!;
#endif
ulong key = ConvertToUInt64(value);
string name = names[i];
string jsonName = FormatJsonName(name, namingPolicy);
_nameCacheForWriting.TryAdd(key, JsonEncodedText.Encode(jsonName, encoder));
_nameCacheForReading?.TryAdd(jsonName, value);
...
}
}
public EnumConverter(EnumConverterOptions converterOptions, JsonNamingPolicy? namingPolicy, JsonSerializerOptions serializerOptions)
{
...
#if NET
string[] names = Enum.GetNames<T>();
T[] values = Enum.GetValues<T>();
#else
string[] names = Enum.GetNames(Type);
Array values = Enum.GetValues(Type);
#endif
Debug.Assert(names.Length == values.Length);
JavaScriptEncoder? encoder = serializerOptions.Encoder;
for (int i = 0; i < names.Length; i++)
{
#if NET
T value = values[i];
#else
T value = (T)values.GetValue(i)!;
#endif
ulong key = ConvertToUInt64(value);
string name = names[i];
string jsonName = FormatJsonName(name, namingPolicy);
_nameCacheForWriting.TryAdd(key, JsonEncodedText.Encode(jsonName, encoder));
_nameCacheForReading?.TryAdd(jsonName, value);
...
}
}
9 replies