InsertJSONStructure Property Name Translation

Just starting out? Need help? Post your questions and find answers here.
User avatar
Crusiatus Black
Enthusiast
Enthusiast
Posts: 389
Joined: Mon May 12, 2008 1:25 pm
Location: The Netherlands
Contact:

InsertJSONStructure Property Name Translation

Post by Crusiatus Black »

Hi there,

This question is out of pure curiosity, however I've been wondering about methods for automatically translating property names to a different case. I would like to be able to name structure fields with the pascal case convention, but json properties with the snake case convention. If you look at the following code, I think many would agree it's a bit undefined behavior-like, however I am wondering if it is a viable solution.

I haven't worked with JSON in PureBasic a lot, so if there's another safer way I'd love to hear.

Code: Select all

; The type behind the pointer of the source/target data on the PB side
Structure Book
  Title.s
  Author.s
  ReleaseYear.i
EndStructure

Structure Person
  FirstName.s
  LastName.s
  Age.l
  List Books.Book()
EndStructure

; The type used for the Structure parameter to use these names on the JSON side
Structure _book_serialization_alias
  title.s
  author.s
  year.i
EndStructure

Structure _person_serialization_alias
  first_name.s
  last_name.s
  age.l
  List books._book_serialization_alias()
EndStructure

Macro AddBook(_P_, _TITLE_, _AUTHOR_, _YEAR_)
  AddElement(_P_\Books())
  _P_\Books()\Title = _TITLE_
  _P_\Books()\Author = _AUTHOR_
  _P_\Books()\ReleaseYear = _YEAR_
EndMacro

Define P.Person
P\FirstName = "John"
P\LastName  = "Smith"
P\Age        = 42

AddBook(P, "Investing For Dummies", "Bill Gates", 2011)
AddBook(P, "English Grammar For Dummies", "Shakespeare", 1590)
AddBook(P, "A Little Bit of Everything For Dummies", "Freddy Mercury", 1980)

If CreateJSON(0)
  ; A pointer to Person, but struct _person_serialization_alias
  InsertJSONStructure(JSONValue(0), @P, _person_serialization_alias)
  Debug ComposeJSON(0, #PB_JSON_PrettyPrint)
EndIf
Image
Bas Groothedde,
Imagine Programming

I live in a philosophical paradoxal randome filled with enigma's!
User avatar
mk-soft
Always Here
Always Here
Posts: 6204
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: InsertJSONStructure Property Name Translation

Post by mk-soft »

It is the only method to work with different names. Here you have to be very careful that the structure is always the same.
Or you can also use the pascal case convention in your program.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Crusiatus Black
Enthusiast
Enthusiast
Posts: 389
Joined: Mon May 12, 2008 1:25 pm
Location: The Netherlands
Contact:

Re: InsertJSONStructure Property Name Translation

Post by Crusiatus Black »

Thank you, that confirms what I assumed.
Image
Bas Groothedde,
Imagine Programming

I live in a philosophical paradoxal randome filled with enigma's!
Post Reply