PlanetSquires Forums

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Win32 Json-c static library for use with Freebasic  (Read 858 times)

Eigil Dingsor

  • Little Newbie
  • *
  • Posts: 18
Win32 Json-c static library for use with Freebasic
« on: December 27, 2019, 04:36:30 AM »

Hi,
I've compiled a static library for Json-c include files that ship with Freebasic. Compiled(GCC) source code corresponds to same Json-c version as stated in include files.
Eigil
Logged

Paul Squires

  • Administrator
  • Guru Member
  • *****
  • Posts: 9386
  • Windows 10
    • PlanetSquires Software
Re: Win32 Json-c static library for use with Freebasic
« Reply #1 on: March 23, 2021, 04:35:14 PM »

Hi Eigil,

I am looking at json libraries and see that you have provided a static interface to the json-c. I simply copied your file to the same folder as my sample source code and added this to the top of the file:

#include once "json-c/json.bi"
#inclib "JsonWin32"

The exe appears to compile but that is where I seem to get stuck. I am not confident that the static code is added to the exe because when I try to output the results of the following get version function I get a compile error saying that the function could not be found.

? json_c_version_num

Maybe I'm missing something simple? I have not used static libraries with FB so I could use a little help. Maybe a simple example of how you are using this code?





Logged
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

Eigil Dingsor

  • Little Newbie
  • *
  • Posts: 18
Re: Win32 Json-c static library for use with Freebasic
« Reply #2 on: March 24, 2021, 05:16:18 AM »

Hi Paul,
Haven't looked at JSON since last February so I had to refresh a bit. Here's a small code sample. I have my static library libjsonwin32.a  in ... \inc\json-c folder.
Code compiles and works on my pc.
Eigil

Code: [Select]
#include once "windows.bi"
#Include once "file.bi"
#include ONCE "string.bi"
'json
#include "C:\FreeBasic\FreeBASIC-1.07.0\inc\json-c\json.bi"
#include "C:\FreeBasic\FreeBASIC-1.07.0\inc\json-c\printbuf.bi"
#inclib   "jsonwin32"

dim jOuter as json_object ptr  = json_object_new_object()
dim jBody as json_object ptr  = json_object_new_object()

    json_object_object_add(jOuter,"argument", jbody)



''  'Creating json strings*/
dim jstring1 as  json_object ptr = json_object_new_string("#XXXX.IMS.Compliance.ValidateSimulatedOrdersArgument")

dim jstring2 as  json_object ptr = json_object_new_string("#Collection(String)")

''  'Form the json object*/
''  'Each of these is like a key value pair*/
  json_object_object_add(jBody,"@odata.type", jstring1)
 
  json_object_object_add(jBody,"OrderIds@odata.type", jstring2)
 
   'create an json object array with containg OrderIDS
 dim jOrdersID as json_object ptr = json_object_new_array()
   
      json_object_array_add(jOrdersID, json_object_new_string("Order_1"))
      json_object_array_add(jOrdersID, json_object_new_string("Order_2"))
 
 dim jOrderarr as json_object ptr = jOrdersID
 'add the array to the body
  json_object_object_add(jBody,"OrderIds",jOrderarr )
   'print the object
  print *json_object_to_json_string_ext(jBody,JSON_C_TO_STRING_PRETTY )
sleep
end



Logged

Paul Squires

  • Administrator
  • Guru Member
  • *****
  • Posts: 9386
  • Windows 10
    • PlanetSquires Software
Re: Win32 Json-c static library for use with Freebasic
« Reply #3 on: March 24, 2021, 07:44:38 AM »

Thanks Eigil !!!

I really appreciate you taking the time to help me out with this one. I now have it working correctly on my end.  :-)

Logged
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

Paul Squires

  • Administrator
  • Guru Member
  • *****
  • Posts: 9386
  • Windows 10
    • PlanetSquires Software
Re: Win32 Json-c static library for use with Freebasic
« Reply #4 on: March 24, 2021, 07:48:21 AM »

Here is the code that I am using thanks to Eigil's example:

Code: [Select]
' json-c test

#include once "json-c/json.bi"
#include once "json-c/printbuf.bi"

' Copy the libJsonWin32.a file to either same folder as your source code/exe or
' to the \lib folder of your GCC toolchain.
#inclib "JsonWin32"


dim jOuter as json_object ptr  = json_object_new_object()
dim jBody as json_object ptr  = json_object_new_object()

json_object_object_add(jOuter,"argument", jbody)


''  'Creating json strings*/
dim jstring1 as  json_object ptr = json_object_new_string("#XXXX.IMS.Compliance.ValidateSimulatedOrdersArgument")
dim jstring2 as  json_object ptr = json_object_new_string("#Collection(String)")

''  'Form the json object*/
''  'Each of these is like a key value pair*/
json_object_object_add(jBody,"@odata.type", jstring1)
 
json_object_object_add(jBody,"OrderIds@odata.type", jstring2)
 
'create an json object array with containg OrderIDS
dim jOrdersID as json_object ptr = json_object_new_array()
   
json_object_array_add(jOrdersID, json_object_new_string("Order_1"))
json_object_array_add(jOrdersID, json_object_new_string("Order_2"))
 
dim jOrderarr as json_object ptr = jOrdersID

'add the array to the body
json_object_object_add(jBody,"OrderIds",jOrderarr )

'print the object
print *json_object_to_json_string_ext(jBody,JSON_C_TO_STRING_PRETTY )

sleep
end

Logged
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

Eigil Dingsor

  • Little Newbie
  • *
  • Posts: 18
Re: Win32 Json-c static library for use with Freebasic
« Reply #5 on: March 24, 2021, 08:04:20 AM »

Paul,
I was a bit too triggerhappy with copy paste.

If you replace the *json_object_to_json_string_ext(jBody,JSON_C_TO_STRING_PRETTY ) line with

Code: [Select]
print *json_object_to_json_string_ext(jOuter,JSON_C_TO_STRING_PRETTY )
you'll see the nested structure better.
\E
Logged