LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   JSON-C and the woes I have... (https://www.linuxquestions.org/questions/programming-9/json-c-and-the-woes-i-have-4175698471/)

ButterflyMelissa 07-29-2021 01:46 PM

JSON-C and the woes I have...
 
Hi all :)
In an effort to learn C, I try to parse JSON data and use it in my program. I use JSON-C for that (see github https://github.com/json-c/json-c ) but the tutorial is nearly impossible for me.
Mostly due to the fact that I dont know any C as well, of course.
Anyone have experience with said lib? Or...can suggest an alternative that can read and write (update settings to persist them) JSON?
Thanks
Melissa

TB0ne 07-29-2021 02:59 PM

Quote:

Originally Posted by ButterflyMelissa (Post 6270753)
Hi all :)
In an effort to learn C, I try to parse JSON data and use it in my program. I use JSON-C for that (see github https://github.com/json-c/json-c ) but the tutorial is nearly impossible for me. Mostly due to the fact that I dont know any C as well, of course. Anyone have experience with said lib? Or...can suggest an alternative that can read and write (update settings to persist them) JSON?

Seems to be two different things here; learning C, and wanting to parse JSON. Obviously, if you don't know C, programming in that language is going to be difficult. I'd suggest starting with either the "jq" Linux utility, which specifically parses JSON, or using either Perl or Python. Either of those languages have less of a learning curve.

Also, saying "read and write" is a bit nebulous...depends on the use case and your data needs.

lovemeslk 07-29-2021 03:14 PM

Have you tried using this tutorial
https://gist.github.com/alan-mushi/19546a0e2c6bd4e059fd

one of the projects I work with use Json in c++
Code:

#include "json/reader.h" // To parse manifest.json from pepperflash

void exportFlashVars()
{
        if(        getenv( "FS_FLASH_PLUGIN" ) && getenv( "FS_FLASH_VERSION" ) ) // User already set those enviroment variables in their shell or in the launcher program
                return;

        char const* pathChecks[] = {
                "/usr/lib64/chromium-browser/PepperFlash/", // Gentoo
                "/usr/lib32/chromium-browser/PepperFlash/", // Gentoo
                "/opt/google/chrome/PepperFlash/", // Mint 17.3 / XBuntu 14.04, probably works with all Ubuntu flavors of that version.
                "/usr/lib/chromium/PepperFlash/", // Slackware
                "/usr/lib64/chromium/PepperFlash/", // Slackware
                "/opt/secondlife-install/PepperFlash/", // In case someone likes to extract pepperflash from a chrome installer on their own, give them a choice with a predef. directory
                NULL
        };

        std::string strExpectedArch = "ia32";
#if ADDRESS_SIZE == 64
        strExpectedArch = "x64";
#endif
       
        for( int i = 0; pathChecks[i]; ++i )
        {
                std::string strPath = pathChecks[i];
                std::string strManifest =  strPath + "manifest.json";
                std::string strPlugin = strPath + "libpepflashplayer.so";
                if( !LLFile::isfile( strManifest ) )
                        continue;
                if( !LLFile::isfile( strPlugin ) )
                        continue;

                std::ifstream file;
                file.open( strManifest, std::ios::in );
                if( !file.is_open() )
                {
                        LL_WARNS( "Flash" ) << "Cannot open " << strManifest << LL_ENDL;
                        continue;
                }
               
                Json::Value root;
                Json::Reader reader;
                if (!reader.parse(file,root))
                        continue;

                std::string strArch; // ia32 or x64
                std::string strVersion;

                if( root.isMember( "version" ) )
                        strVersion = root[ "version" ].asString();
                if( root.isMember( "x-ppapi-arch" ) )
                        strArch = root[ "x-ppapi-arch" ].asString();
                       
                if( strExpectedArch != strArch )
                {
                        LL_WARNS( "Flash" ) << "Arch mismatch expecting: " << strExpectedArch << " got: " << strArch << LL_ENDL;
                        continue;
                }

                LL_INFOS( "Flash" ) << "Arch: " << strArch << " version: " << strVersion << " plugin " << strPlugin << LL_ENDL;
       
                setenv( "FS_FLASH_PLUGIN", strPlugin.c_str(), 0 );
                setenv( "FS_FLASH_VERSION", strVersion.c_str(), 0 );
                break;
        }
}

This is an old snippet of how we used to get flash player to work on CEF3 embedded into the linux secondlife viewer.
but for just learning C you may want to look at programs how they implement read parse write.
the code was written by NickyD team lead for Firestorm secondlife client.
We needed json because we had to parse the pepperflash manifest.json for version and turn that into a variable.
Quote:

We parsed the manifest.json got the version
because the call to the browser is pepperflash-version then we set the paths libpepflashplayer.so
flashplayer is in the past. Hope this helps.

Skaperen 07-29-2021 08:51 PM

if your goal is to learn C then i suggest not trying to parse any JSON or using most non-standard libraries. most of them have you work with pointers in complex ways. and a typical JSON library assumes reading (or writing) files in whole.

if your goal is to parse JSON then C is a poor choice of language to learn. i suggest Perl or Python. i have a few scripts i have written that work with JSON or compressed JSON, all done in Python. i reserve using C for special cases, now (since learning Python a few years ago), and JSON is not one of them. i just cannot imagine ever doing any JSON in C.

if your goal is to acquire career skills, then consider JSON and C to be "either/or". just about any job that needs either will not need the other.

ButterflyMelissa 07-30-2021 12:38 PM

Thanks all,
The goal is (perhaps) weirder...but...I may, as TBOne seems to hint, break things down in learning one thing at a time.
The ultimate goal: C, NCurses and then to persist local data on disk. JSON seemed a logical choice (from my point of view which clearly is incomplete). The aim of JSON was simply to persist some settings locally. XML could be an option too, or flat text...
It's not a carreer choice/option at my age anymore. I have 57 candles on my particular cake...not much carreer left ;)
As lovemeslk suggested...Python. And...why not, really...
I have some things to meditate over :)
Thanks and hugs all round
Melissa

EdGr 07-31-2021 08:03 AM

The C libraries' fopen, fprintf, and fscanf routines provide an easy way to write and read files. These should be among the first library calls that a C programmer learns.
Ed

sundialsvcs 08-02-2021 09:31 AM

I think that you'd find it considerably easier – in quite a number of ways – to use C++ here.

In particular, C++ gives you a taxonomy of "objects," which are a very-natural way to express the data structures that are inherent in a JSON stream. It also gives you much-more-sophisticated primitive data objects, such as a well-behaved string type. And the efficiency is just as good as "C." It's just a heck of a lot easier to use! :) ("Whenever possible, always let the language do the dirty work ...")

ntubski 08-03-2021 04:29 PM

Quote:

Originally Posted by sundialsvcs (Post 6271673)
I think that you'd find it considerably easier – in quite a number of ways – to use C++ here.

IMO, learning C++ instead of C will be much harder, not easier.

shruggy 08-04-2021 02:56 AM

Quote:

Originally Posted by ButterflyMelissa (Post 6270997)
I have some things to meditate over :)

Master Foo and the Ten Thousand Lines

ButterflyMelissa 08-05-2021 12:41 PM

Quote:

Originally Posted by shruggy (Post 6272400)

Thank you...I stand enlightened... :)

sundialsvcs 08-07-2021 05:37 PM

I suppose it's up to you whether you consider C++ "hard to learn," but I now find it to be much easier. The C++ language makes it very easy for you to use more sophisticated data-types – such as a well-designed string type – as well as a rich library of "containers" and other classes. The implementation is efficient, but the language does more work for you.

GazL 08-10-2021 05:42 AM

I guess it depends on how deep you go. C++ language itself is not that much more complex than C, and having the std::string certainly helps, but as soon as you start getting involved with OO, the STL stuff, vectors, iterators and all that fancy jazz things take on a far more complex turn.

Also, if the OP is planning to learn ncurses, then probably best to stick with C which is its native binding.


For the OP, rather than jumping straight into parsing more complex/structured formats like json or xml, I'd start with simple key=value format datafiles, which are nice and easy to parse with standard C library functions such as strtok() or even just using old-school fscanf() and fprintf() as EdGr suggested above.

Walk before you run...
Oh, and listen to Master Foo. ;)

mark707 08-25-2021 04:24 PM

String handling in C is not trivial, Especially since there is no native string data type in C. I suggest you take your time with C and do your JSON parsing with a scripting language.


All times are GMT -5. The time now is 10:50 AM.