Comments (11)
Yes, there is a way to do it. Stand by for example...
from json-fortran.
@jacobwilliams thanks, looking forward to it!
from json-fortran.
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.
@jacobwilliams I will give this approach a shot and let you know how it goes.
from json-fortran.
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.
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.
yeah exactly what I was thinking… probably best to leave it alone for now.
btw, auto-deploy almost done :-)
from json-fortran.
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.
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.
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.
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)
- null_to_integer_mode mode
- Available in Fedora 35/36 HOT 2
- Cross-platform? HOT 2
- On Definition of operator [] HOT 1
- How to get a derived-type table? HOT 2
- Make unicode support optional for homebrew HOT 1
- Rounding a real when printing HOT 1
- Incorrect mod locations when using FetchContent HOT 3
- Fails to build on PPC due to missing ieee_arithmetic in gfortran HOT 2
- Tests fail with nvfortran compiler HOT 10
- Many tests unable to find executable, several tests fail (PowerPC): what am I doing wrong here? HOT 12
- Check if keyword is valid HOT 1
- Add test to FPM file
- Unable to build json-fortran with nvfortran HOT 1
- libdir and includedir empty in json-fortran.pc HOT 2
- All tests fail with errors like "Could not find executable ./jf_test_45" HOT 2
- json parse failed HOT 1
- Can not find some variables in json file HOT 1
- Write out 2d array to JSON file HOT 3
- Question: dealing with duplicate named objects HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from json-fortran.