ParseJSON Unexpected end of input error (4K limit)

Just starting out? Need help? Post your questions and find answers here.
endo
Enthusiast
Enthusiast
Posts: 141
Joined: Fri Apr 30, 2004 10:44 pm
Location: Turkiye (istanbul)
Contact:

ParseJSON Unexpected end of input error (4K limit)

Post 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?
-= endo (registered user of purebasic since 98) =-
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: ParseJSON Unexpected end of input error (4K limit)

Post 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
quidquid Latine dictum sit altum videtur
normeus
Enthusiast
Enthusiast
Posts: 470
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Re: ParseJSON Unexpected end of input error (4K limit)

Post by normeus »

I think it's a Friday the 13th thing ( bad Jason ) :twisted:

Code: Select all

If ParseJSON(0, Chr(34) + Space(1000000) )
  Debug Len(GetJSONString(JSONValue(0)))
Else
  Debug JSONErrorMessage()
EndIf
Norm
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
endo
Enthusiast
Enthusiast
Posts: 141
Joined: Fri Apr 30, 2004 10:44 pm
Location: Turkiye (istanbul)
Contact:

Re: ParseJSON Unexpected end of input error (4K limit)

Post 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.
-= endo (registered user of purebasic since 98) =-
infratec
Always Here
Always Here
Posts: 7577
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: ParseJSON Unexpected end of input error (4K limit)

Post 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)

Code: Select all

JsonParser < test.json
Bernd
Last edited by infratec on Mon Jan 25, 2016 7:53 am, edited 1 time in total.
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: ParseJSON Unexpected end of input error (4K limit)

Post 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.
quidquid Latine dictum sit altum videtur
infratec
Always Here
Always Here
Posts: 7577
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: ParseJSON Unexpected end of input error (4K limit)

Post by infratec »

Changed my listing above.

Bernd
endo
Enthusiast
Enthusiast
Posts: 141
Joined: Fri Apr 30, 2004 10:44 pm
Location: Turkiye (istanbul)
Contact:

Re: ParseJSON Unexpected end of input error (4K limit)

Post 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
-= endo (registered user of purebasic since 98) =-
Post Reply