1

我有一个在运行时生成的具有 x 个属性的 json,因此我不能使用 POCO 来反序列化它,例如

"{"UserId": 1234,"Name": "Adnan","Age": 30, "Salary": 3500.65}"

对我来说最好的选择是将它反序列化Dictionary<string,object>

我正在使用 ServiceStackJsonSerializer将 json 反序列化为可以正常工作的 a Dictionary<string,object>,但是当我尝试获取字典中对象的类型时,它们不会相应地匹配。

我尝试了以下方法:

如果我不使用以下选项,所有字典对象值都被推断为strings

JsConfig.ConvertObjectTypesIntoStringDictionary = true;
JsConfig.TryToParsePrimitiveTypeValues = true;

当我使用上述选项时,所有Int64Double值都被推断为Decimal值。

是否有任何选项可以更改它,以便将原始值推断为Int64Double代替Decimal

Ps:我没有要求它必须是确切的类型,即Int32它是否属于该括号。

我已经尝试使用 Json.Net 并且它工作正常并且对象值被推断为Int64and Double,但是当我JsonSerializer在我的项目中使用 ServiceStack 时,很高兴知道如何使用它来实现这一点。

4

1 回答 1

2

TryToParseNumericType = true

JsConfig.TryToParseNumericType = true如果您希望 ServiceStack.Text 确定类型之外的类型,则需要设置decimal

控制原始类型:

关于控制您的数字被解析到的类型,我提交了更改以改进 ServiceStack.Text,以提供将包含在即将发布的版本中的此类功能,并提交

所以你将能够做到:

JsConfig.TryParseNumericType = true;
JsConfig.ParsePrimitiveFloatingPointTypes = ParseAsType.Single;
JsConfig.ParsePrimitiveIntegerTypes = ParseAsType.Int32 | ParseAsType.Int64;

正如您所指出的那样,该配置将返回 aInt32而不是bytea 。而不是decimal你会得到一个float.

以供参考:

下面是更新的原始解析方法,它现在应该提供更好的控制,以及在无法解析类型时更好的回退选项。

public static object ParsePrimitive(string value)
{
    if (string.IsNullOrEmpty(value)) return null;

    bool boolValue;
    if (bool.TryParse(value, out boolValue)) return boolValue;

    // Parse as decimal
    decimal decimalValue;
    var acceptDecimal = JsConfig.ParsePrimitiveFloatingPointTypes.HasFlag(ParseAsType.Decimal);
    var hasDecimal = decimal.TryParse(value, NumberStyles.Number, CultureInfo.InvariantCulture, out decimalValue);

    // Check if the number is an Primitive Integer type given that we have a decimal
    if(hasDecimal && decimalValue == decimal.Truncate(decimalValue))
    {
        // Value is a whole number
        if (JsConfig.ParsePrimitiveIntegerTypes.HasFlag(ParseAsType.Byte) && decimalValue <= byte.MaxValue && decimalValue >= byte.MinValue) return (byte)decimalValue;
        if (JsConfig.ParsePrimitiveIntegerTypes.HasFlag(ParseAsType.SByte) && decimalValue <= sbyte.MaxValue && decimalValue >= sbyte.MinValue) return (sbyte)decimalValue;
        if (JsConfig.ParsePrimitiveIntegerTypes.HasFlag(ParseAsType.Int16) && decimalValue <= Int16.MaxValue && decimalValue >= Int16.MinValue) return (Int16)decimalValue;
        if (JsConfig.ParsePrimitiveIntegerTypes.HasFlag(ParseAsType.UInt16) && decimalValue <= UInt16.MaxValue && decimalValue >= UInt16.MinValue) return (UInt16)decimalValue;
        if (JsConfig.ParsePrimitiveIntegerTypes.HasFlag(ParseAsType.Int32) && decimalValue <= Int32.MaxValue && decimalValue >= Int32.MinValue) return (Int32)decimalValue;
        if (JsConfig.ParsePrimitiveIntegerTypes.HasFlag(ParseAsType.UInt32) && decimalValue <= UInt32.MaxValue && decimalValue >= UInt32.MinValue) return (UInt32)decimalValue;
        if (JsConfig.ParsePrimitiveIntegerTypes.HasFlag(ParseAsType.Int64) && decimalValue <= Int64.MaxValue && decimalValue >= Int64.MinValue) return (Int64)decimalValue;
        if (JsConfig.ParsePrimitiveIntegerTypes.HasFlag(ParseAsType.UInt64) && decimalValue <= UInt64.MaxValue && decimalValue >= UInt64.MinValue) return (UInt64)decimalValue;
        return null;
    }

    // Value is a floating point number

    // Return a decimal if the user accepts a decimal
    if(hasDecimal && acceptDecimal)
        return decimalValue;

    // Parse as double if decimal failed or user wants a double
    double doubleValue = 0;
    var acceptDouble = JsConfig.ParsePrimitiveFloatingPointTypes.HasFlag(ParseAsType.Double);
    var hasDouble = (!hasDecimal || acceptDouble) && double.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out doubleValue);

    // Return a double if the user accepts a double
    if(acceptDouble && hasDouble)
        return doubleValue;

    // Parse as float
    float floatValue;
    var acceptFloat = JsConfig.ParsePrimitiveFloatingPointTypes.HasFlag(ParseAsType.Single);
    var hasFloat = float.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out floatValue);

    // Return a float if the user accepts a float
    if(acceptFloat && hasFloat)
        return floatValue;

    // Default to decimal, then double , then float or null
    if(hasDecimal) return decimalValue;
    if(hasDouble) return doubleValue;
    if(hasFloat) return floatValue;
    return null;
}
于 2014-08-13T16:36:12.903 回答