%{
#define ATSCODEFORMAT "txt"
#if (ATSCODEFORMAT == "txt")
#include "utils/atsdoc/HATS/postiatsatxt.hats"
#endif // end of [ATSCCODEFORMAT]
val _thisfilename = atext_strcst"jansson.cats"
val () = theAtextMap_insert_str ("thisfilename", _thisfilename)
fun atscntrb_jansson_define (fname: string): atext = (
  atext_strptr (sprintf ("#define atscntrb_jansson_%s %s", @(fname, fname)))
) // end of [atscntrb_jansson_define]
%}\
/*
** Copyright (C) 2010 Chris Double.
**
** Permission to use, copy, modify, and distribute this software for any
** purpose with or without fee is hereby granted, provided that the above
** copyright notice and this permission notice appear in all copies.
** 
** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
** WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
** MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
** ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
** WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
** ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
** OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#atscode_separator_for_C()

/*
** Time: September, 2012
** Author Hongwei Xi (gmhwxi AT gmail DOT com)
**
** The API is simplied a bit in the hope that it can be used more easily.
*/

#atscode_separator_for_C()

\#ifndef JANSSON_JANSSON_CATS
\#define JANSSON_JANSSON_CATS

#atscode_separator_for_C()

\#include <jansson.h>

#atscode_separator_for_C()

typedef json_t *JSONptr ;
typedef const json_t *JSONconstptr ;

#atscode_separator_for_C()

#atscntrb_jansson_define("json_typeof")

#atscode_separator_for_C()

\#define \\
atscntrb_jansson_json_is_null(x) json_is_null((JSONptr)x)
\#define \\
atscntrb_jansson_json_is_true(x) json_is_true((JSONptr)x)
\#define \\
atscntrb_jansson_json_is_false(x) json_is_false((JSONptr)x)

\#define \\
atscntrb_jansson_json_is_boolean(x) json_is_boolean((JSONptr)x)

\#define \\
atscntrb_jansson_json_is_integer(x) json_is_integer((JSONptr)x)
\#define \\
atscntrb_jansson_json_is_real(x) json_is_real((JSONptr)x)
\#define \\
atscntrb_jansson_json_is_number(x) json_is_number((JSONptr)x)

\#define \\
atscntrb_jansson_json_is_string(x) json_is_string((JSONptr)x)

\#define \\
atscntrb_jansson_json_is_array(x) json_is_array((JSONptr)x)

\#define \\
atscntrb_jansson_json_is_object(x) json_is_object((JSONptr)x)

#atscode_separator_for_C()

#atscntrb_jansson_define("json_incref")
#atscntrb_jansson_define("json_decref")

#atscode_separator_for_C()

#atscntrb_jansson_define("json_null")
#atscntrb_jansson_define("json_true")
#atscntrb_jansson_define("json_false")

#atscode_separator_for_C()

#atscntrb_jansson_define("json_integer")
#atscntrb_jansson_define("json_integer_value")
#atscntrb_jansson_define("json_integer_set")

#atscode_separator_for_C()

#atscntrb_jansson_define("json_real")
#atscntrb_jansson_define("json_real_value")
#atscntrb_jansson_define("json_real_set")

#atscode_separator_for_C()

#atscntrb_jansson_define("json_number_value")

#atscode_separator_for_C()

#atscntrb_jansson_define("json_string")
#atscntrb_jansson_define("json_string_nocheck")

\#define \\
atscntrb_jansson_json_string_value(x) ((char*)json_string_value(x))

#atscntrb_jansson_define("json_string_set")
#atscntrb_jansson_define("json_string_set_nocheck")

#atscode_separator_for_C()

#atscntrb_jansson_define("json_array")

#atscntrb_jansson_define("json_array_size")

#atscode_separator_for_C()

#atscntrb_jansson_define("json_array_get")

#atscode_separator_for_C()

ATSinline()
atstype_ptr
atscntrb_jansson_json_array_get_exnmsg
(
 atstype_ptr json, atstype_size ind, atstype_ptr msg
) {
  JSONptr itm ;
  itm = atscntrb_jansson_json_array_get (json, ind) ;
  if (!itm) {
    fprintf (stderr, "exit(ATS): json_array_get: %s\n", (char*)msg) ; exit (1);
  } // end of [if]
  return itm ;
} // end of [atscntrb_jansson_json_array_get_exnmsg]

#atscode_separator_for_C()

ATSinline()
atstype_ptr
atscntrb_jansson_json_array_get1
(
  atstype_ptr json, atstype_size ind
) {
  JSONptr itm ;
  itm = json_array_get((JSONptr)json, ind);
  if (itm) json_incref(itm) ;
  return (itm) ;
} // end of [atscntrb_jansson_json_array_get1]

ATSinline()
atstype_ptr
atscntrb_jansson_json_array_get1_exnmsg
(
  atstype_ptr json, atstype_size ind, atstype_ptr msg
) {
  JSONptr itm ;
  itm = atscntrb_jansson_json_array_get1 (json, ind) ;
  if (!itm) {
    fprintf (stderr, "exit(ATS): json_array_get1: %s\n", (char*)msg) ; exit (1);
  } // end of [if]
  return (itm) ;
} // end of [atscntrb_jansson_json_array_get1_exnmsg]

#atscode_separator_for_C()

#atscntrb_jansson_define("json_array_set")
#atscntrb_jansson_define("json_array_set_new")

#atscntrb_jansson_define("json_array_append")
#atscntrb_jansson_define("json_array_append_new")

#atscntrb_jansson_define("json_array_insert")
#atscntrb_jansson_define("json_array_insert_new")

#atscntrb_jansson_define("json_array_remove")

#atscntrb_jansson_define("json_array_clear")

#atscntrb_jansson_define("json_array_extend")

#atscode_separator_for_C()

#atscntrb_jansson_define("json_object")

#atscntrb_jansson_define("json_object_size")

#atscode_separator_for_C()

#atscntrb_jansson_define("json_object_get")

ATSinline()
atstype_ptr
atscntrb_jansson_json_object_get_exnmsg
(
 atstype_ptr json, atstype_ptr key, atstype_ptr msg
) {
  JSONptr itm ;
  itm = atscntrb_jansson_json_object_get (json, key) ;
  if (!itm) {
    fprintf (stderr, "exit(ATS): json_object_get: %s\n", (char*)msg) ; exit (1);
  } // end of [if]
  return itm ;
} // end of [atscntrb_jansson_json_object_get_exnmsg]

#atscode_separator_for_C()

ATSinline()
atstype_ptr
atscntrb_jansson_json_object_get1
(
  atstype_ptr json, atstype_ptr key
) {
  JSONptr itm ;
  itm = atscntrb_jansson_json_object_get (json, key) ;
  if (itm) json_incref(itm) ;
  return (itm) ;
} // end of [atscntrb_jansson_json_object_get1]

ATSinline()
atstype_ptr
atscntrb_jansson_json_object_get1_exnmsg
(
  atstype_ptr json, atstype_ptr key, atstype_ptr msg
) {
  JSONptr itm ;
  itm = atscntrb_jansson_json_object_get1 (json, key) ;
  if (!itm) {
    fprintf (stderr, "exit(ATS): json_object_get1: %s\n", (char*)msg) ; exit (1);
  } // end of [if]
  return (itm) ;
} // end of [atscntrb_jansson_json_object_get1_exnmsg]

#atscode_separator_for_C()

#atscntrb_jansson_define("json_object_set")
#atscntrb_jansson_define("json_object_set_nocheck")
#atscntrb_jansson_define("json_object_set_new")
#atscntrb_jansson_define("json_object_set_new_nocheck")

#atscntrb_jansson_define("json_object_del")

#atscntrb_jansson_define("json_object_clear")

#atscntrb_jansson_define("json_object_update")
#atscntrb_jansson_define("json_object_update_existing")
#atscntrb_jansson_define("json_object_update_missing")

#atscode_separator_for_C()

#atscntrb_jansson_define("json_object_iter")
#atscntrb_jansson_define("json_object_iter_at")
#atscntrb_jansson_define("json_object_iter_next")
#atscntrb_jansson_define("json_object_iter_nextret")

#atscode_separator_for_C()

#atscntrb_jansson_define("json_object_iter_key")
#atscntrb_jansson_define("json_object_iter_value")

#atscntrb_jansson_define("json_object_iter_set")
#atscntrb_jansson_define("json_object_iter_set_new")

#atscntrb_jansson_define("json_object_key_to_iter")

#atscode_separator_for_C()

#atscntrb_jansson_define("json_loads")
#atscntrb_jansson_define("json_loadb")
#atscntrb_jansson_define("json_loadf")
#atscntrb_jansson_define("json_load_file")

#atscntrb_jansson_define("json_dumps")
#atscntrb_jansson_define("json_dumpf")
#atscntrb_jansson_define("json_dump_file")

#atscode_separator_for_C()

\#endif // ifndef JANSSON_JANSSON_CATS

#atscode_separator_for_C()

#atscode_eof_strsub_for_C("\#thisfilename$")\

%{
implement main () = fprint_filsub (stdout_ref, "jansson_atxt.txt")
%}\
