Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add serialization example #11

Open
minhqdao opened this issue Apr 11, 2023 · 6 comments
Open

Add serialization example #11

minhqdao opened this issue Apr 11, 2023 · 6 comments
Labels
documentation Improvements or additions to documentation

Comments

@minhqdao
Copy link
Contributor

minhqdao commented Apr 11, 2023

Please add an example on how to build a JSON from scratch and serialize it.

@awvwgk awvwgk added the documentation Improvements or additions to documentation label Apr 11, 2023
@awvwgk
Copy link
Member

awvwgk commented Apr 11, 2023

For serialization the json_serialize, json_dump, and json_dumps procedures can be used. The simplest way is to print or write the return value of json_serialize:

  block
    use jonquil, only : json_serialize
    character(:), allocatable :: str

    str = json_serialize(object)
    print '(a)', str
  end block

To create the JSON object, have a look in the set_value function provided by TOML Fortran.

  block
    use jonquil, only : json_object, set_value
    type(json_object) :: object
    type(json_object), pointer :: child
    call set_value(object, "val", 1.0)
    call set_value(object, "sub", child)
    call set_value(child, "val", 2.0)
    ! ...
  end block

@minhqdao
Copy link
Contributor Author

Thanks. I found the new_object procedure to create a JSON:

program main
  use jonquil

  implicit none

  type(json_object) :: json

  call new_object(json)

  call set_value(json, 'hello', 'world')

  print *, json_serialize(json)

end program

However, the output using gfortran is:

 ,"hello": "world"}

Is it maybe a bug? 🤔

@minhqdao
Copy link
Contributor Author

Same result using json_dumps.

@awvwgk
Copy link
Member

awvwgk commented Apr 11, 2023

The comma is produced in

visitor%output = visitor%output // ","

Looks like I transcribed this incorrectly from

https://github.com/toml-f/toml-f/blob/2f04799adf9ecfff3f7a2a58e89496fd6556b21d/test/compliance/json_ser.f90#L211

which would produce an opening brace. So definitely a bug.

@awvwgk
Copy link
Member

awvwgk commented Apr 11, 2023

Fixed the serialization error. Let me know if you find any other discrepancies.

@minhqdao
Copy link
Contributor Author

Looks good, thanks for the very quick fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants