Unexpected ConvertTo-Json results? Answer: it has a default -Depth of 2

问题

Why do I get unexpected ConvertTo-Json results?
And why does a round-trip ($Json | ConvertFrom-Json | ConvertTo-Json) fail?

Meta issue

Stackoverflow has a good mechanism to prevent duplicate questions but as far as I can see there is no mechanism to prevent questions that have a duplicate cause.
Take this question as a an example: almost every week a new question
comes in with the same cause, yet it is often difficult to define it as a
duplicate because the question itself is just a slightly different.
Nevertheless, I wouldn't be surprised if this question/answer itself
ends up as a duplicate (or off-topic) but unfortunately stackoverflow
has no possibility to write an article to prevent other programmers from continuing writing questions caused by this “known” pitfall.

Duplicates

A few examples of similar questions with the same common cause:

Different

So, were does this “self-answered” question differ from the above duplicates?
It has the common cause in the title and with that it might better prevent repeating questions due to the same cause.

 
解答

Answer

ConvertTo-Json has a -Depth parameter:

Specifies how many levels of contained objects are included in the JSON representation.
The default value is 2.

Example

To do a full round-trip with a JSON file you need to increase the -Depth for the ConvertTo-Json cmdlet:

$Json | ConvertFrom-Json | ConvertTo-Json -Depth 9

TL;DR

Probably because ConvertTo-Json terminates branches that are deeper than the default -Depth (2) with a (.Net) full type name, programmers assume a bug or a cmdlet limitation and do not read the help or about.
Personally, I think a string with a simple ellipsis (three dots: …) at the end of the cut off branch, would have a clearer meaning (see also: Github issue: 8381)

Why?

This issue often ends up in another discussion as well: Why is the depth limited at all?

Some objects have circular references, meaning that a child object
could refer to a parent (or one of its grandparents) causing a
infinitive loop if it would be serialized to JSON.

Take for example the following hash table with a parent property that refers to the object itself:

$Test = @{Guid = New-Guid}
$Test.Parent = $Test

If you execute: $Test | ConvertTo-Json it will conveniently stop at a depth level of 2 by default:

{
"Guid": "a274d017-5188-4d91-b960-023c06159dcc",
"Parent": {
"Guid": "a274d017-5188-4d91-b960-023c06159dcc",
"Parent": {
"Guid": "a274d017-5188-4d91-b960-023c06159dcc",
"Parent": "System.Collections.Hashtable"
}
}
}

This is why it is not a good idea to automatically set the -Depth to a large amount.

Unexpected ConvertTo-Json results? Answer: it has a default -Depth of 2的更多相关文章

  1. angularjs SyntaxError: Unexpected token  in JSON at position 0

    使用NodeJs读取json格式的文件,转换成对象时报错 :SyntaxError: Unexpected token in JSON at position 0,这个问题查了两三个小时,记录一下解决 ...

  2. uncaught syntaxerror unexpected token U JSON

    uncaught syntaxerror unexpected token U JSON The parameter for the JSON.parse may be returning nothi ...

  3. oracle已知会导致错误结果的bug列表(Bug Issues Known to cause Wrong Results)

    LAST UPDATE:     1 Dec 15, 2016 APPLIES TO:     1 2 3 4 Oracle Database - Enterprise Edition - Versi ...

  4. 【转】C# 解析JSON方法总结

    http://blog.csdn.net/jjhua/article/details/51438317 主要参考http://blog.csdn.NET/joyhen/article/details/ ...

  5. 《Python网络编程》学习笔记--使用谷歌地理编码API获取一个JSON文档

    Foundations of Python Network Programing,Third Edition <python网络编程>,本书中的代码可在Github上搜索fopnp下载 本 ...

  6. (转载)Newtonsoft.Json使用总结

    Newtonsoft.Json使用总结 初识JSON.......................................................................... ...

  7. Json.net 常用使用小结

    using System; using System.Linq; using System.Collections.Generic; namespace microstore { public int ...

  8. PHP 解析 ElasticSearch 的 json 方法,有關遍歷所有 json 元素

    以下是eleasticsearch返回的json資料:{"took" : 12,"timed_out" : false,"_shards" ...

  9. pickle和json模块

    json模块 json模块是实现序列化和反序列化的,主要用户不同程序之间的数据交换,首先来看一下: dumps()序列化 import json '''json模块是实现序列化和反序列话功能的''' ...

随机推荐

  1. CSS中为什么有的元素能够设置高度,而有的元素却不能设置高度与宽度?

    可以使用{display:block}将内联元素变为块级元素,同时使用{display:inline}将块级元素变为内联元素. {display:inline-block}又是怎么回事,根据张鑫旭老师 ...

  2. js小功能2:切换

    HTML: <div id="tb"> <ul><li class="on">房产</li><li> ...

  3. vue防重复点击(指令实现)

    快速点击按钮会重复多次调用接口,防止出现这样的情况 全局定义,方便调用 新建plugins.js export default { install (Vue) { // 防重复点击(指令实现) Vue ...

  4. 理解Cookie和Session的区别及使用

    资料一: Cookie和Session的区别 共同之处: cookie和session都是用来跟踪浏览器用户身份的绘画方式. 区别: cookie数据保存在客户端,session数据保存在服务端. s ...

  5. php--常见算法3

    <?php function leijia($number){ $arr=[]; for($i=1;$i<=$number;$i++) { for($j=1;$j<=$number; ...

  6. 第八章· MySQL日志管理

    一.MySQL日志简介  二.错误日志 1.作用: 记录mysql数据库的一般状态信息及报错信息,是我们对于数据库常规报错处理的常用日志. 2.默认位置: $MYSQL_HOME/data/ 3.开 ...

  7. while 循环 及 and or not

    一  while 循环 语法: while 条件: 结果 如果条件为真,则直接执行结果,然后再次判断条件,直到条件是假,停止循环 结束循环: 1.改变条件 2.break 二 流程控制 break 和 ...

  8. 在macOS下启用CGO_ENABLED的交叉编译

    在macOS下启用CGO_ENABLED的交叉编译 在启用CGO_ENABLED的情况下,尝试使用下面命令进行Windows平台的交叉编译: $ CGO_ENABLED= GOOS=windows G ...

  9. JAVA WEB初接触——简单的MVC架构

    1.概述 之前有过开发web的经验,因此我不会向无头苍蝇一般,心里还是有点数的

  10. MySQL 异地 双机房同步之otter

    一.背景: 阿里巴巴B2B公司,因为业务的特性,卖家主要集中在国内,买家主要集中在国外,所以衍生出了杭州和美国异地机房的需求, 同时为了提升用户体验,整个机房的架构为双A,两边均可写,由此诞生了ott ...