Blitz3D+ Command Reference

JsonErrorOffset:Long ( )

Parameters

None.

Description

Returns the zero-based UTF-8 byte offset of the last JSON failure, or -1.

Where JsonErrorLine and JsonErrorColumn speak editor language, JsonErrorOffset speaks byte language: how many UTF-8 bytes into the source ParseJson or LoadJson stopped. That is the right coordinate for seeking in raw data - jump a file stream to the damage, or slice the bytes around the failure into a debug log.

Two conversions to keep straight: the offset counts bytes, not characters, so multi-byte UTF-8 text shifts it away from what Len-style counting suggests; and it is zero-based while Mid is one-based, so the offending character of a source string is Mid(src,offset+1,1).

It is one of five companions set together on failure - JsonError has the message, JsonErrorPath the logical path. When no position is available it returns -1 (note: not 0, which is a valid offset). A successful parse or load clears the family; the value is context-local.

For the full rules, see the Application Data language reference.

Requires Extended mode.

See also: JsonError, JsonErrorLine, JsonErrorColumn, JsonErrorPath, ParseJson.

Example

; JsonErrorOffset Example
; -----------------------
; Requires Extended mode.

q$=Chr(34)  ; Double-quote character, for building JSON text

; A one-line save-game corrupted mid-stream: 12x5 is not a number
src$="{"+q+"player"+q+":"+q+"Aria"+q+","+q+"gold"+q+":12x5}"

Print "Parsing this broken save-game:"
Print src
Print ""

save=ParseJson(src)
Print "ParseJson returned "+save+" (0 = failed): "+JsonError()
Print ""

; JsonErrorOffset returns the ZERO-based UTF-8 byte offset of the
; error (-1 when unavailable) - useful for seeking in raw data
offset=JsonErrorOffset()
Print "JsonErrorOffset: "+offset
Print ""

; Show the character sitting at that offset (Mid is one-based)
Print "Character at offset "+offset+": '"+Mid(src,offset+1,1)+"'"

Print ""
Print "Press any key to close the example"
WaitKey

End

Index