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