Coder Social home page Coder Social logo

Comments (11)

jacobwilliams avatar jacobwilliams commented on May 28, 2024

Yes, there is a way to do it. Stand by for example...

from json-fortran.

kmanalo avatar kmanalo commented on May 28, 2024

@jacobwilliams thanks, looking forward to it!

from json-fortran.

jacobwilliams avatar jacobwilliams commented on May 28, 2024

OK, this might be what you need. The idea is you have to create an empty array object, and then add the strings one at a time. Then you add this array to your main json structure. There is no need to put the allocatable string into a temp string. Code snippet below.

type(json_value),pointer :: array  !temp for creating the array

call json_create_array(array, 'char_array')  !create an empty array

!add each element (note: name is not used)
do i=1,size(derived_type_array)
  call json_add(array, '', derived_type_array(i)%str)  ![this is json_value_add_string]
end do

call json_add(outp, array)  !add the array to the output [this is json_value_add_member]
nullify(array) !don't need this anymore

Note: I haven't tried to compile a working example, so if you can't get it to work, let me know. I think this should work. Also: be aware that Gfortran still has some bugs related to the allocatable character strings... I've got several workarounds in json-fortran to account for them, but it's possible you could be seeing one of them in your code..not sure.

from json-fortran.

kmanalo avatar kmanalo commented on May 28, 2024

@jacobwilliams I will give this approach a shot and let you know how it goes.

from json-fortran.

zbeekman avatar zbeekman commented on May 28, 2024

I wonder if it would be worth the hassle to add some kind of direct ragged edge string array API… I can’t quite picture what it would look like at the moment, without publicizing a new ragged edge string array derived type, which is likely beyond the scope of this project.

from json-fortran.

jacobwilliams avatar jacobwilliams commented on May 28, 2024

I think it would have to look like @kmanalo's string type above. Then we would have json_get, etc. routines for arrays of this type. I actually thought about something like this before, but decided against it. Perhaps one day, Fortran will let us have arrays of allocatable strings...

from json-fortran.

zbeekman avatar zbeekman commented on May 28, 2024

yeah exactly what I was thinking… probably best to leave it alone for now.

btw, auto-deploy almost done :-)

from json-fortran.

kmanalo avatar kmanalo commented on May 28, 2024

I tried your suggestion and it worked out very well.

Thanks, this was helpful in a non-string context also, in working with double precision arrays. I had a similar need to avoid the array constructor there also.

Also, I'm a dummy, this may be more of a semantic issue: every time I read the comment "don't need this anymore" in the suffix to nullify(), I totally thought that meant that one didn't need to use nullify anymore. I get it now.

from json-fortran.

jacobwilliams avatar jacobwilliams commented on May 28, 2024

Ha ha! Yeah, I was just saying that the pointer is no longer needed once the data it is pointing to has been added to the outp structure. I nullify it just to be safe (for example, you might want to reuse it later on). Note that the json_destroy routine deallocates the memory and destroys the linked list, which is not appropriate here for array (since the data itself is now part of outp), but would be appropriate to call for outp, when you are finished with it, in order to avoid a memory leak.

Happy to help!

from json-fortran.

zbeekman avatar zbeekman commented on May 28, 2024

Speaking of memory leaks, I wonder what it would take to get a reference counting scheme going, for better automatic memory management… it seems that there is a rather large burden on the end user to ensure that they cleanup after themselves to avoid memory leaks, but at the same time ensure that they don’t create any dangling pointers.

from json-fortran.

jacobwilliams avatar jacobwilliams commented on May 28, 2024

For the json_file class it might be fairly easy I think (we could even try to use a final method...although last time I tried that it was buggy). However, if you are using json_value pointers to build your own structure, I'm not sure how that would work...it could be tricky. It may be possible...but I've not thought in detail how that would work with Fortran pointers.

from json-fortran.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.