Page 1 of 1
ParseJSON Unexpected end of input error (4K limit)
Posted: Fri Jan 22, 2016 7:02 pm
by endo
ParseJSON fails with "Unexpected end of input error" message if the given string is longer than 4096 byte.
Is there any chance to increase this limit?
Re: ParseJSON Unexpected end of input error (4K limit)
Posted: Fri Jan 22, 2016 7:53 pm
by freak
Do you have an example?
This works as expected and is much longer than 4k:
Code: Select all
If ParseJSON(0, Chr(34) + Space(1000000) + Chr(34))
Debug Len(GetJSONString(JSONValue(0)))
Else
Debug JSONErrorMessage()
EndIf
Re: ParseJSON Unexpected end of input error (4K limit)
Posted: Fri Jan 22, 2016 11:16 pm
by normeus
I think it's a Friday the 13th thing ( bad Jason )
Code: Select all
If ParseJSON(0, Chr(34) + Space(1000000) )
Debug Len(GetJSONString(JSONValue(0)))
Else
Debug JSONErrorMessage()
EndIf
Norm
Re: ParseJSON Unexpected end of input error (4K limit)
Posted: Sun Jan 24, 2016 5:35 pm
by endo
Here is my whole application code,
It reads from console data or program arguments, decode the json string and pretty-print it to console output.
I use it like:
Code: Select all
more myfile.json | json.exe
or
json.exe < myfile.json
or
json.exe <some-json>
or
curl myurl | json.exe
All the above works if the given string is not longer than 4096 bytes
Code: Select all
If OpenConsole()
If CountProgramParameters() > 0
val.s = ProgramParameter()
Else
*Buffer = AllocateMemory(32768)
ReadSize = ReadConsoleData(*Buffer,32768)
If ReadSize = 32768
PrintN("Buffer overflow (max 32KB)")
FreeMemory(*Buffer)
CloseConsole()
End
ElseIf ReadSize > 0
val = Trim(PeekS(*Buffer,ReadSize,#PB_Ascii))
EndIf
EndIf
If CreateJSON(0)
If ParseJSON(0,val,#PB_JSON_NoCase)
val = ComposeJSON(0, #PB_JSON_PrettyPrint)
PrintN(val)
SetClipboardText(val)
Else
PrintN("> " + JSONErrorMessage())
PrintN(Str(Len(val)))
PrintN(val)
EndIf
EndIf
CloseConsole()
EndIf
I tried with/without compiling unicode exe and #PB_Ascii / #PB_UTF8 flags for PEEKS.
Re: ParseJSON Unexpected end of input error (4K limit)
Posted: Sun Jan 24, 2016 8:23 pm
by infratec
Hm...,
this:
Code: Select all
EnableExplicit
Define Val$, *Buffer, ReadSize.i
If OpenConsole()
If CountProgramParameters() > 0
Val$ = ProgramParameter()
Else
*Buffer = AllocateMemory(1024)
If *Buffer
Repeat
ReadSize = ReadConsoleData(*Buffer, 1024)
If ReadSize > 0
val$ = Trim(PeekS(*Buffer, ReadSize, #PB_Ascii))
EndIf
Until ReadSize = 0
FreeMemory(*Buffer)
EndIf
EndIf
If Len(Val$) > 0
If CreateJSON(0)
If ParseJSON(0, val$, #PB_JSON_NoCase)
val$ = ComposeJSON(0, #PB_JSON_PrettyPrint)
PrintN(val$)
SetClipboardText(val$)
Else
PrintN("Error> " + JSONErrorMessage())
PrintN("-------------")
PrintN("Length: " + Str(Len(val$)))
PrintN(val$)
EndIf
FreeJSON(0)
EndIf
EndIf
CloseConsole()
EndIf
works for me with a file of 4657 bytes. (win7 x64 PB 5.41 x86 compiled as unicode)
Bernd
Re: ParseJSON Unexpected end of input error (4K limit)
Posted: Sun Jan 24, 2016 10:23 pm
by freak
Your problem is that ReadConsoleData() may not return all data in one call. You have to use a loop. The help for ReadConsoleData() contains an example how to do this.
Re: ParseJSON Unexpected end of input error (4K limit)
Posted: Mon Jan 25, 2016 7:54 am
by infratec
Changed my listing above.
Bernd
Re: ParseJSON Unexpected end of input error (4K limit)
Posted: Mon Jan 25, 2016 2:13 pm
by endo
@freak: Got it! Thank you. It solved my problem.
Here is my Pretty Print Json dos command source code, very useful to test web services that returns json
Code: Select all
#maxSize = 64 ;64KB
If OpenConsole()
If CountProgramParameters() > 0
val.s = ProgramParameter()
Else
pos = 0
*Buffer = AllocateMemory(1024 * #maxSize)
For i=1 To #maxSize
ReadSize = ReadConsoleData(*Buffer + pos, 1024)
pos + ReadSize
If ReadSize = 0
Break
EndIf
Next
If i = #maxSize
PrintN("Buffer overflow, max: " + Str(#maxSize))
FreeMemory(*Buffer)
CloseConsole()
End
EndIf
val = Trim(PeekS(*Buffer,pos,#PB_UTF8))
EndIf
If val <> "" And CreateJSON(0)
If ParseJSON(0,val,#PB_JSON_NoCase)
val = ComposeJSON(0, #PB_JSON_PrettyPrint)
PrintN(val)
SetClipboardText(val)
Else
PrintN("Error: " + JSONErrorMessage())
EndIf
Else
PrintN("Error: Cannot initialize JSON object or empty input.")
EndIf
CloseConsole()
EndIf