Unintended custom keywords behaviour

Share your advanced PureBasic knowledge/code with the community.
User avatar
whax
New User
New User
Posts: 1
Joined: Fri Dec 11, 2020 4:50 pm

Unintended custom keywords behaviour

Post by whax »

Hi,
While parsing some third party JSONs with a list object named "data" I found a weird workaround which is probably a bug, but I'm using it as a feature.

When I add the word "data" (lowercase) to my custom Keywords in the IDE this program will work as intended, otherwise it will not.
Quick example:

Code: Select all

EnableExplicit

;# Struct
Structure sItemData
  Val.i
EndStructure

Structure sItem
  Id.i
  List data.sItemData()
EndStructure

;# Global
Global NewList item.sItem()
Global *json_b = UTF8("W3siSWQiOjAsImRhdGEiOlt7IlZhbCI6NX0seyJWYWwiOjQ1fSx7IlZhbCI6NTl9LHsiVmFsIjo0OX0seyJWYWwiOjI0fV19LHsiSWQiOjEsImRhdGEiOlt7IlZhbCI6NDJ9LHsiVmFsIjo5M30seyJWYWwiOjkxfV19LHsiSWQiOjIsImRhdGEiOlt7IlZhbCI6ODB9LHsiVmFsIjoyOX0seyJWYWwiOjEyfV19LHsiSWQiOjMsImRhdGEiOlt7IlZhbCI6NDV9LHsiVmFsIjoyM31dfSx7IklkIjo0LCJkYXRhIjpbeyJWYWwiOjM4fSx7IlZhbCI6NTd9LHsiVmFsIjoxOH0seyJWYWwiOjYwfV19LHsiSWQiOjUsImRhdGEiOlt7IlZhbCI6OTd9LHsiVmFsIjoyMn0seyJWYWwiOjk0fSx7IlZhbCI6NTN9XX0seyJJZCI6NiwiZGF0YSI6W3siVmFsIjo1OX0seyJWYWwiOjY2fSx7IlZhbCI6Mzh9XX0seyJJZCI6NywiZGF0YSI6W3siVmFsIjowfSx7IlZhbCI6NzZ9LHsiVmFsIjo1OH1dfSx7IklkIjo4LCJkYXRhIjpbeyJWYWwiOjF9LHsiVmFsIjoyMX0seyJWYWwiOjYwfV19LHsiSWQiOjksImRhdGEiOlt7IlZhbCI6NTh9LHsiVmFsIjo3fV19LHsiSWQiOjEwLCJkYXRhIjpbeyJWYWwiOjM2fSx7IlZhbCI6NDl9LHsiVmFsIjo2NH0seyJWYWwiOjk0fSx7IlZhbCI6Mjh9XX1d")
Global.s json$ = PeekS(*json_b, MemorySize(*json_b), #PB_UTF8)
Global *json_e = AllocateMemory(MemorySize(*json_b)) : Base64Decoder(json$, *json_e, MemorySize(*json_b))
Global.s json_d$ = PeekS(*json_e, -1, #PB_UTF8)
Global.i obj = ParseJSON(#PB_Any, json_d$)

;# Following code will always be negative if "data" has not been set as a custom keyword in the IDE Settings (Editor > Coloring > Custom keywords)
If obj
  ExtractJSONList(JSONValue(obj), item())
  ForEach item()
    If ListSize(item()\Data())
      Debug Str(item()\Id) + " item()\data() List found, size: " + ListSize(item()\data())
    Else
      Debug Str(item()\Id) + " item()\data() List NOT found"
    EndIf
  Next item()
  Debug Chr(10) + ComposeJSON(obj, #PB_JSON_PrettyPrint)
EndIf

DisableExplicit
This not specific to the IDE, as the pbcompiler cli will also respect the custom keywords.

Results:
If data keyword has been added.
If no data keyword has been added.
infratec
Always Here
Always Here
Posts: 6818
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Unintended custom keywords behaviour

Post by infratec »

The "bug/feature" comes from an other behavior.

The normal syntax 'correction' makes Data out of data inside the structure.
So it is not found inside the JSON.

To avoid this you can add #PB_JSON_NoCase to ParseJSON().
I think that's the better 'solution'.

You can ask such 'bug' questions in the 'Coding Question' section.
Post Reply