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. Linux--目录属性

    目录的读属性:表示具有读取目录结构清单的权限.使用ls命令可以将该目录中的文件和子目录的内容列出来. 目录的写属性:表示具有更改目录结构清单的权限.包括以下操作: 建立新的文件与目录 删除已经存在的文 ...

  2. ceres for Android 太慢的解决方法

    跨平台编译了ceres,结果在android平台上运行的太慢,优化一次要0.3秒左右,时不时要一两秒.这太扯了.没辙了,在google上瞎搜索,看到 Jacobian evaluation is ve ...

  3. 语义分割之车道线检测Lanenet(tensorflow版)

    Lanenet 一个端到端的网络,包含Lanenet+HNet两个网络模型,其中,Lanenet完成对车道线的实例分割,HNet是一个小网络结构,负责预测变换矩阵H,使用转换矩阵H对同属一条车道线的所 ...

  4. python常用模块:logging、hashlib、re

    今日内容主要有:一.logging模块二.logging模块的使用三.hashlib模块四.re模块 一.logging模块 import logging # 1 日志的级别 logging.debu ...

  5. Asp.Net Zero通用打印实现

    Asp.Net Zero是一款非常优秀的web框架,可以用来快速构建业务系统.框架满足了业务系统所需的大部分通用功能,但是系统必须的打印报表功能一直没有实现.下面给大家介绍如何在zero中集成打印功能 ...

  6. PAT Basic 1022 D进制的A+B (20 分)

    输入两个非负 10 进制整数 A 和 B (≤),输出 A+B 的 D (1)进制数. 输入格式: 输入在一行中依次给出 3 个整数 A.B 和 D. 输出格式: 输出 A+B 的 D 进制数. 输入 ...

  7. Beta冲刺版本第一天

    该作业所属课程:https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass2 作业要求地址:https://edu.cnblogs.com ...

  8. 网络协议相关面试问题-DNS相关面试问题

    对于网络上的大部通讯都是基于TCP/IP协议的, 其中最重要的是IP协议,它是基于IP地址的,而计算机通讯只能识别IP地址,如192.168.0.1,而不能识别像咱们在浏览器敲得见名之义的" ...

  9. gluOrtho2D与glViewport

    https://blog.csdn.net/HouraisanF/article/details/83444183 窗口与显示主要与三个量有关:世界坐标,窗口大小和视口大小.围绕这些量共有4个函数: ...

  10. Mysql索引类型分析

    一.简介   MySQL目前主要有以下几种索引类型:   1.普通索引   2.唯一索引   3.主键索引   4.组合索引   5.全文索引   二.语句 CREATE TABLE table_na ...