Blitz3D+ Command Reference

JsonEqual ( left,right )

Parameters

left - first JSON handle to compare

right - second JSON handle to compare

Description

Tests two JSON values for structural equality.

Two values are equal when they hold the same kind of data with the same content, all the way down. The classic use is change detection: keep a CopyJson snapshot of the last saved state, and compare against the live document to decide whether to show "unsaved changes" or skip an autosave.

The comparison is strict about kinds: integer 1 and number 1.0 are NOT equal, because JSON_INTEGER and JSON_NUMBER are different kinds. Among numbers, 0.0 and -0.0 are also distinguished. Strings compare by exact bytes with no normalization.

Arrays are equal when they have the same elements in the same order. Objects are equal when they have the same set of keys with equal values - insertion order does not matter for equality, even though it is preserved everywhere else. Comparing a handle against itself is always true, and comparing values of different kinds is simply false, never an error.

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

Requires Extended mode.

See also: CopyJson, JsonKind, JsonStringify, SaveJsonAtomic.

Example

; JsonEqual Example
; -----------------
; Requires Extended mode.

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

; Compare a save-game against its backup to detect unsaved changes
save=ParseJson("{"+q+"player"+q+":"+q+"Aria"+q+","+q+"gold"+q+":1250}")
backup=CopyJson(save)

Print "Fresh copy - JsonEqual(save,backup) = "+JsonEqual(save,backup)

; Spend some gold, then compare again
v=CreateJsonInteger(200L)
JsonObjectSet save,"gold",v
FreeJson v
Print "After spending gold       - equal = "+JsonEqual(save,backup)
Print ""

; JsonEqual is strict: integer 1 and number 1.0 are NOT equal
int_one=CreateJsonInteger(1L)
num_one=CreateJsonNumber(1.0D)
Print "Integer 1 vs number 1.0   - equal = "+JsonEqual(int_one,num_one)

FreeJson num_one
FreeJson int_one
FreeJson backup
FreeJson save

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

End

Index