Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

powershell - ConvertFrom-Json max length

I have a problem using PowerShell v3 when converting JSON strings over 2MB in size. The default limit in JSON serializer used by PowerShell is set to 2MB which explains the error.

However when I deserialize object using ConvertFrom-Json on a smaller set (I got various data objects with smaller and bigger internal collections but those are the same objects) it returns very nice object with all properties which I can easily access.

To overcome the limitations of the serializer I tried to deserialize data by hand:

$jsser = New-Object System.Web.Script.Serialization.JavaScriptSerializer
$jsser.MaxJsonLength = $jsser.MaxJsonLength * 10
$jsser.RecursionLimit = 99    

$outObject = $jsser.DeserializeObject($json)

The object looks differently it seems that internal collections were not deserialized and when I try to execute properties they return empty results.

My questions:

  1. Assumption is ConvertFrom-Json does some additional magic or somehow creates a template for the object before serialization. Any idea how to replicate it?

  2. The object I get is always a PSCustomObject; if I get the object I want setup by ConvertFrom-Json is there anyway to use it as object type in JsonSerializer?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I had the same problem and was able to solve it like this:

[void][System.Reflection.Assembly]::LoadWithPartialName("System.Web.Extensions")        
$jsonserial= New-Object -TypeName System.Web.Script.Serialization.JavaScriptSerializer 
$jsonserial.MaxJsonLength  = 67108864
$Obj = $jsonserial.DeserializeObject($CourseTypesResponse)

You can use $jsonserial.MaxJsonLength to manipulate the MaxJsonLength property

source: https://social.technet.microsoft.com/Forums/windowsserver/en-US/833c99c1-d8eb-400d-bf58-38f7265b4b0e/error-when-converting-from-json?forum=winserverpowershell&prof=required


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...