Json.Net中强类型的Object []

时间:2015-01-31 23:59:54

标签: c# arrays json object types

我有一个问题,它给我带来了很多麻烦。我使用JSON.Net来序列化/反序列化对象。 我使用此代码通过其参数类型调用方法。

如果我运行以下代码,我会得到一个对象[] {int,int}

然后我使用Json.Net进行序列化/反序列化,在此过程之后它转向object [] {long,long}

现在我的问题:如何更改以下代码以保留类型和值?

 static class Program
{
    static byte[] ObjectAsByteArray(object data)
    {
        string text = JsonConvert.SerializeObject(data, Formatting.None, new JsonSerializerSettings()
        {
            Formatting=Formatting.Indented,
            TypeNameHandling = TypeNameHandling.All,
            TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple
        });

        return Encoding.UTF8.GetBytes(text);
    }

    static T ByteArrayAsObject<T>(byte[] data)
    {
        Object answer = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(data), typeof(T), new JsonSerializerSettings()
        {
            Formatting = Formatting.Indented,
            TypeNameHandling = TypeNameHandling.All,
            TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple
        });
        return (T)answer;
    }

    static int Multiply(int a, int b)
    {
        return a * b;
    }


    static object[] ArgumentsOf(Expression<Action> expression)
    {
        MethodCallExpression outermostExpression = expression.Body as MethodCallExpression;
        object[] Params = outermostExpression.Arguments.Cast<ConstantExpression>().Select(x => x.Value).ToArray();

        return Params;
    }


    [STAThread]
    static void Main()
    {
        object[] Arguments = ArgumentsOf(() => Multiply(5, 100));
        byte[] ArgAsByte = ObjectAsByteArray(Arguments);

        object[] DeserializedArguments = ByteArrayAsObject<object[]>(ArgAsByte);

    }




}

1 个答案:

答案 0 :(得分:0)

你需要将ArgumentsOf更改为泛型,否则返回类型是System.Object的数组,它序列化为

"$type": "System.Object[], mscorlib",

但是,如果指定特定类型的int,它将序列化为:

"$type": "System.Int32[], mscorlib",

这样的事情:

static T[] ArgumentsOf<T>(Expression<Func<T>> expression)
{
    MethodCallExpression outermostExpression = expression.Body as MethodCallExpression;
    var Params = outermostExpression.Arguments.Cast<ConstantExpression>().Select(x => (T)x.Value).ToArray();
    return Params;
}

然后整个样本看起来都是这样的:

static class Program
{
static byte[] ObjectAsByteArray(object data)
{
    string text = JsonConvert.SerializeObject(data, Newtonsoft.Json.Formatting.None, new JsonSerializerSettings()
    {
        Formatting=Newtonsoft.Json.Formatting.Indented,
        TypeNameHandling = TypeNameHandling.All,
        TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple
    });

    return Encoding.UTF8.GetBytes(text);
}

static T ByteArrayAsObject<T>(byte[] data)
{
    var answer = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(data), typeof(T), new JsonSerializerSettings()
    {
        Formatting = Newtonsoft.Json.Formatting.Indented,
        TypeNameHandling = TypeNameHandling.All,
        TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple
    });
    return (T)answer;
}

static int Multiply(int a, int b)
{
    return a * b;
}


static T[] ArgumentsOf<T>(Expression<Func<T>> expression)
{
    MethodCallExpression outermostExpression = expression.Body as MethodCallExpression;
    var Params = outermostExpression.Arguments.Cast<ConstantExpression>().Select(x => (T)x.Value).ToArray();
    return Params;
}


[STAThread]
static void Main()
{
    var Arguments = ArgumentsOf(() => Multiply(5, 100));
    byte[] ArgAsByte = ObjectAsByteArray(Arguments);

    var DeserializedArguments = ByteArrayAsObject<int[]>(ArgAsByte);
}

}