Question Tamara Lebedeva · 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 <MAXSTRING> error. Increasing of MaxLocalLengt isn't solution. Is there some good way parse big JSON?

Comments

Dmitry Maslennikov · Aug 22, 2016

You should not read stream which you got from request, just pass it to $fromJSON, because it understand streams too.

0
Timothy Leavitt · Aug 22, 2016

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.

0
Stefan Wittmann · Aug 22, 2016

Try passing in the stream object. You are operating on a string, that is why you are getting MAXSTRING errors. This should work:

TRY
{
Set RequestObj = ##class(%Object).$fromJSON(%request.Content)
} CATCH(Exception) {
Set Status=Exception.AsStatus()
}
0
Tamara Lebedeva  Aug 22, 2016 to Stefan Wittmann

Yes, I've tried it. It works on version 2016.1.1 but doesn't work on 2016.1.0. Is update only right way?

0
Jon Willeke  Aug 23, 2016 to Tamara Lebedeva

$fromJSON(stream) works for me in 2016.1.0. What problem are you having?

0