Recent

Author Topic: JSON to FPC Unit?  (Read 1067 times)

peardox

  • Jr. Member
  • **
  • Posts: 70
JSON to FPC Unit?
« on: July 09, 2020, 02:22:35 pm »
A while ago I found this... http://pgeorgiev.com/introducing-jsontodelphiclass/ and the associated git https://github.com/PKGeorgiev/Delphi-JsonToDelphiClass

These, basically, take some sample JSON and a Delphi unit is created to handle live data. This appears to work in a limited sense. When I say limited I mean that real-world data APIs have a habit of evolving and the JSON you can realistically expect from some API is frequently missing some field or another.

In the worst cases fields can change type (that's not fun).

I've got the basics of JSON Schema analysis working OK and now I want to take the results of the analysis and automate the generation of FPC units based on that analysis.

The analysis code takes a series of JSON records and then builds a list of key = 'json type' objects. I also want to make the types more reflect the arbitrary data processed so that for example whereas JSON has the concept of a number I can decide if a number is an integer or real for example (there are quite a lot of type conversions that can be automated)

Any pointers on automating the creation of the end-product unit code based on the results of the analysed json?



Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: JSON to FPC Unit?
« Reply #1 on: July 09, 2020, 02:34:50 pm »
Doesn't the API have a documentation?

Usually you shouldn't build your interfaces from the json data you get, but from a well defined documentation that exactly states which values each member can hold, if it is optional or required and so on

mr-highball

  • Full Member
  • ***
  • Posts: 233
    • Highball Github
Re: JSON to FPC Unit?
« Reply #2 on: July 09, 2020, 02:52:48 pm »
A while ago I started down the route of doing this but didn't get too much interest to continue developing it.
See this forum post,
https://forum.lazarus.freepascal.org/index.php/topic,43609.msg305469.html#msg305469

Here's a link to the line of the test app that shows putting some json in.
https://github.com/mr-highball/json2pas/blob/dc9230bc4a3024ed20c77817ab579e2fc0d0e058/test/json2pas_util.lpr#L67

To finish things up it wouldnt take "too much" work but I've been focusing on other things. If I get some more interest I could focus a little more on it, but as is you could clone the repo and put in some custom json on that line and generate code and possibly use this as a starting point for your own.
One of the nice things about mine over the generation tool you mentioned is that it supports inferring inheritance based on the structure of the json, so if you put in "parent" json then "child" json then the class structure generated would show that.

peardox

  • Jr. Member
  • **
  • Posts: 70
Re: JSON to FPC Unit?
« Reply #3 on: July 09, 2020, 04:09:52 pm »
Doesn't the API have a documentation?

Usually you shouldn't build your interfaces from the json data you get, but from a well defined documentation that exactly states which values each member can hold, if it is optional or required and so on

Ahh, well documented API Nirvana ...

This only really works if you control both the API creation and consumption.

REST APIs are frequently an after-thought with poor or absent documentation.

peardox

  • Jr. Member
  • **
  • Posts: 70
Re: JSON to FPC Unit?
« Reply #4 on: July 09, 2020, 04:13:36 pm »
@mr-highball

Thanks, I'll go check that out

 

TinyPortal © 2005-2018