Question
· Aug 22, 2016

$fromJSON and MAXSTRING

Hi, all.
I have CSP application and it needs to get and process data from ajax request with json-content. JSON can be very big.
In this case:
TRY
{
Set RequestObj = ##class(%Object).$fromJSON(%request.Content.Read())
} CATCH(Exception) {
Set Status=Exception.AsStatus()
}
I get just part of getting JSON and validate error in $fromJSON.
If I try to read it all in cycle:
TRY
{
While (%request.Content.AtEnd = 0) {
Set Data=Data_%request.Content.Read()
}
Set RequestObj = ##class(%Object).$fromJSON(Data)
} CATCH(Exception) {
Set Status=Exception.AsStatus()
}
I get error.
Increasing of MaxLocalLengt isn't solution.
Is there some good way parse big JSON?

Discussion (6)0
Log in or sign up to continue

You can pass a stream to $fromJSON instead of a string:

USER>set tStream = ##class(%Stream.TmpCharacter).%New()
 
USER>d tStream.Write("{""a"":2}")
 
USER>s obj = {}.$fromJSON(tStream)
 
USER>w obj.a
2

In your case:

Set RequestObj = ##class(%Object).$fromJSON(%request.Content)

This is much easier than reading the stream into a string first, and avoids <MAXLENTH> issues.