Skip to content

Commit

Permalink
Add BoolsAsInts property
Browse files Browse the repository at this point in the history
  • Loading branch information
G33kDude committed Aug 15, 2021
1 parent 574519b commit 8dd6893
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
16 changes: 16 additions & 0 deletions Src/cJson.ahk
Expand Up @@ -3,6 +3,22 @@ class cJson
{
static version := "0.3.0-git-dev"

BoolsAsInts[]
{
get
{
this._init()
return NumGet(this.lib.bBoolsAsInts, "Int")
}

set
{
this._init()
NumPut(value, this.lib.bBoolsAsInts, "Int")
return value
}
}

_init()
{
if (this.lib)
Expand Down
28 changes: 22 additions & 6 deletions Src/loads.c
Expand Up @@ -392,18 +392,34 @@ int loads(short **ppJson, VARIANT *pResult)
{
expect_str("true");

pResult->vt = VT_DISPATCH;
pResult->pdispVal = objTrue;
objTrue->lpVtbl->AddRef(objTrue);
if (bBoolsAsInts)
{
pResult->vt = VT_I4;
pResult->llVal = 1;
}
else
{
pResult->vt = VT_DISPATCH;
pResult->pdispVal = objTrue;
objTrue->lpVtbl->AddRef(objTrue);
}
return 0;
}
else if (**ppJson == 'f') //////////////////////////////////////////// false
{
expect_str("false");

pResult->vt = VT_DISPATCH;
pResult->pdispVal = objFalse;
objFalse->lpVtbl->AddRef(objFalse);
if (bBoolsAsInts)
{
pResult->vt = VT_I4;
pResult->llVal = 0;
}
else
{
pResult->vt = VT_DISPATCH;
pResult->pdispVal = objFalse;
objFalse->lpVtbl->AddRef(objFalse);
}
return 0;
}
else if (**ppJson == 'n') ///////////////////////////////////////////// null
Expand Down
3 changes: 3 additions & 0 deletions Src/shared.h
Expand Up @@ -22,4 +22,7 @@ MCL_EXPORT_GLOBAL(objNull);
static IDispatch *fnGetObj;
MCL_EXPORT_GLOBAL(fnGetObj);

static bool bBoolsAsInts = false;
MCL_EXPORT_GLOBAL(bBoolsAsInts);

#endif
8 changes: 8 additions & 0 deletions Tests/LoadsTestSuite.ahk
Expand Up @@ -64,10 +64,18 @@ class LoadsTestSuite

Test_Array_Specials()
{
cJson.BoolsAsInts := False
expected := [cJson.true, cJson.false, cJson.null]
produced = [true, false, null]
produced := cJson.Loads(produced)
Yunit.assert(isEqual(produced, expected), Format(this.message, expected, produced))

cJson.BoolsAsInts := True
expected := [1, 0, cJson.null]
produced = [true, false, null]
produced := cJson.Loads(produced)
Yunit.assert(isEqual(produced, expected), Format(this.message, expected, produced))
cJson.BoolsAsInts := False
}

Test_Array_Nested()
Expand Down

0 comments on commit 8dd6893

Please sign in to comment.