21
novembre
2012
json-glib-sharp : c# bindings for the libjson-glib-1.0
novembre
2012
Un article de Rachel
1 Commentaire
During my work on a media library, I was looking how to work with json. Instead of searching the net yet another library, I saw that I could use libjson-glib with existing gapi utility.
First, you must write an source.xml file in this format :
<gapi-parser-input>
<api filename="api.xml">
<library name="libjson-glib-1.0.so">
<namespace name="Json">
<dir>/usr/include/json-glib-1.0/json-glib</dir>
</namespace>
</library>
</api>
</gapi-parser-input>
<api filename="api.xml">
<library name="libjson-glib-1.0.so">
<namespace name="Json">
<dir>/usr/include/json-glib-1.0/json-glib</dir>
</namespace>
</library>
</api>
</gapi-parser-input>
type these commands in a terminal :
gapi2-parser source.xml
gapi2-codegen --outdir=json-glib --generate api.xml
gapi2-codegen --outdir=json-glib --generate api.xml
Now you have two choices :
- either you call
GLib.GType.Init();
at the beginning of the application
- or you must add a new source file in the newly created folder that will initialize the library, Application.cs :
using System;
using System.Runtime.InteropServices;
namespace Json
{
public class Application
{
[DllImport("gobject-2.0")]
static extern void g_type_init ();
public static void Init(){
g_type_init();
}
}
}So as to call the function this way
Json.Application.Init();
Finally you need to type this command in a terminal :
mcs -pkg:glib-sharp-2.0 -target:library -out:json-glib-sharp.dll json-glib/*.cs
the library can be found in the current folder.
with .Net 4.5 , mono integrates System.Json built from the library NewtomSoft.Json