$fromJSON and MAXSTRING

Caché, JSON, CSP

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?

  • 0
  • 0
  • 690
  • 3
  • 3

Answers

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

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.

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()
}

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?

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

Comments