为什么调用SerializeObject会将String类的成员与我的对象一起序列化?

时间:2014-12-12 18:35:18

标签: json.net powershell-v2.0

请考虑以下示例代码:

$dll = "C:\path\to\NewtonsoftPackage\lib\net35\Newtonsoft.Json.dll"
[Reflection.Assembly]::LoadFile( $dll ) | Out-Null

Function Test {
  param ( $item )
  $settings = new-object Newtonsoft.Json.JsonSerializerSettings
  $settings.ReferenceLoopHandling = 'Ignore'
  $data = [Newtonsoft.Json.JsonConvert]::SerializeObject( $item, [Newtonsoft.Json.Formatting]::None, $settings )
  return $data
}

Function GetItem {
  return "magicString"
}


Test @{test=GetItem; nested = @{ 'stuff' = GetItem}; other = @( 'pants', 'bacon' ); }

我希望输出看起来像这样:

{"nested":{"stuff":"magicString"},"other":["pants","bacon"],"test":"magicString"}

相反,我得到了上述内容,但是我们拥有" magicString",我有很多关于字符串类的数据,比如每个方法(和重载),所以它看起来像这样:

{
    "other": ["pants",
    "bacon"],
    "nested": {
        "stuff": {
            "Members": [{
                "IsSettable": false,
                "IsGettable": true,
                "OverloadDefinitions": ["char Chars(int index) {get;}"],
                "TypeNameOfValue": "System.Char",
                "MemberType": 512,
                "Name": "Chars",
                "IsInstance": true
            }, ... ]
        }
     },
     "test" : {
        "Members": [{
            "MemberType": 4,
            "Value": 4,
            "IsSettable": false,
            "IsGettable": true,
            "TypeNameOfValue": "System.Int32",
            "Name": "Length",
            "IsInstance": true
        }, ... ]
     }
}

为什么SerializeObject()会给我一个带有所有字符串类的json字符串'会员职能?

0 个答案:

没有答案