/* JsonUtils.c generated by valac 0.56.19, the Vala compiler
 * generated from JsonUtils.vala, do not modify */

/*
 * Copyright © 2026 Alain M. (https://github.com/alainm23/planify)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA
 *
 * Authored by: Alain M. <alainmh23@gmail.com>
 */

#include "planify.h"
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <json-glib/json-glib.h>
#include <glib-object.h>
#include <gobject/gvaluecollector.h>

#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif

#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _json_object_unref0(var) ((var == NULL) ? NULL : (var = (json_object_unref (var), NULL)))
typedef struct _UtilsParamSpecJsonUtils UtilsParamSpecJsonUtils;

struct _UtilsParamSpecJsonUtils {
	GParamSpec parent_instance;
};

static gpointer utils_json_utils_parent_class = NULL;

static void utils_json_utils_finalize (UtilsJsonUtils * obj);
static GType utils_json_utils_get_type_once (void);

static gpointer
_json_object_ref0 (gpointer self)
{
	return self ? json_object_ref (self) : NULL;
}

JsonObject*
utils_json_utils_get_object (const gchar* data)
{
	JsonParser* parser = NULL;
	JsonParser* _tmp0_;
	JsonNode* _tmp3_;
	JsonObject* _tmp4_;
	JsonObject* _tmp5_;
	GError* _inner_error0_ = NULL;
	JsonObject* result;
	g_return_val_if_fail (data != NULL, NULL);
	_tmp0_ = json_parser_new ();
	parser = _tmp0_;
	{
		json_parser_load_from_data (parser, data, (gssize) -1, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		GError* _tmp1_;
		const gchar* _tmp2_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp1_ = e;
		_tmp2_ = _tmp1_->message;
		g_debug ("JsonUtils.vala:29: %s", _tmp2_);
		_g_error_free0 (e);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		_g_object_unref0 (parser);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return NULL;
	}
	_tmp3_ = json_parser_get_root (parser);
	_tmp4_ = json_node_get_object (_tmp3_);
	_tmp5_ = _json_object_ref0 (_tmp4_);
	result = _tmp5_;
	_g_object_unref0 (parser);
	return result;
}

JsonObject*
utils_json_utils_get_object_member (const gchar* data,
                                    const gchar* member)
{
	JsonObject* _tmp0_;
	JsonObject* _tmp1_;
	JsonObject* _tmp2_;
	JsonObject* _tmp3_;
	JsonObject* _tmp4_;
	JsonObject* result;
	g_return_val_if_fail (data != NULL, NULL);
	g_return_val_if_fail (member != NULL, NULL);
	_tmp0_ = utils_json_utils_get_object (data);
	_tmp1_ = _tmp0_;
	_tmp2_ = json_object_get_object_member (_tmp1_, member);
	_tmp3_ = _json_object_ref0 (_tmp2_);
	_tmp4_ = _tmp3_;
	_json_object_unref0 (_tmp1_);
	result = _tmp4_;
	return result;
}

gchar*
utils_json_utils_get_string (const gchar* data,
                             const gchar* member)
{
	JsonObject* obj = NULL;
	JsonObject* _tmp0_;
	gboolean _tmp1_ = FALSE;
	JsonObject* _tmp2_;
	gchar* _tmp7_;
	gchar* result;
	g_return_val_if_fail (data != NULL, NULL);
	g_return_val_if_fail (member != NULL, NULL);
	_tmp0_ = utils_json_utils_get_object (data);
	obj = _tmp0_;
	_tmp2_ = obj;
	if (json_object_has_member (_tmp2_, member)) {
		JsonObject* _tmp3_;
		_tmp3_ = obj;
		_tmp1_ = !json_object_get_null_member (_tmp3_, member);
	} else {
		_tmp1_ = FALSE;
	}
	if (_tmp1_) {
		JsonObject* _tmp4_;
		const gchar* _tmp5_;
		gchar* _tmp6_;
		_tmp4_ = obj;
		_tmp5_ = json_object_get_string_member (_tmp4_, member);
		_tmp6_ = g_strdup (_tmp5_);
		result = _tmp6_;
		_json_object_unref0 (obj);
		return result;
	}
	_tmp7_ = g_strdup ("");
	result = _tmp7_;
	_json_object_unref0 (obj);
	return result;
}

gint64
utils_json_utils_get_int (const gchar* data,
                          const gchar* member)
{
	JsonObject* obj = NULL;
	JsonObject* _tmp0_;
	gboolean _tmp1_ = FALSE;
	JsonObject* _tmp2_;
	gint64 result;
	g_return_val_if_fail (data != NULL, -1LL);
	g_return_val_if_fail (member != NULL, -1LL);
	_tmp0_ = utils_json_utils_get_object (data);
	obj = _tmp0_;
	_tmp2_ = obj;
	if (json_object_has_member (_tmp2_, member)) {
		JsonObject* _tmp3_;
		_tmp3_ = obj;
		_tmp1_ = !json_object_get_null_member (_tmp3_, member);
	} else {
		_tmp1_ = FALSE;
	}
	if (_tmp1_) {
		JsonObject* _tmp4_;
		_tmp4_ = obj;
		result = json_object_get_int_member (_tmp4_, member);
		_json_object_unref0 (obj);
		return result;
	}
	result = (gint64) 0;
	_json_object_unref0 (obj);
	return result;
}

gboolean
utils_json_utils_get_bool (const gchar* data,
                           const gchar* member)
{
	JsonObject* obj = NULL;
	JsonObject* _tmp0_;
	gboolean _tmp1_ = FALSE;
	JsonObject* _tmp2_;
	gboolean result;
	g_return_val_if_fail (data != NULL, FALSE);
	g_return_val_if_fail (member != NULL, FALSE);
	_tmp0_ = utils_json_utils_get_object (data);
	obj = _tmp0_;
	_tmp2_ = obj;
	if (json_object_has_member (_tmp2_, member)) {
		JsonObject* _tmp3_;
		_tmp3_ = obj;
		_tmp1_ = !json_object_get_null_member (_tmp3_, member);
	} else {
		_tmp1_ = FALSE;
	}
	if (_tmp1_) {
		JsonObject* _tmp4_;
		_tmp4_ = obj;
		result = json_object_get_boolean_member (_tmp4_, member);
		_json_object_unref0 (obj);
		return result;
	}
	result = FALSE;
	_json_object_unref0 (obj);
	return result;
}

gboolean
utils_json_utils_has_member (const gchar* data,
                             const gchar* member)
{
	JsonObject* _tmp0_;
	JsonObject* _tmp1_;
	gboolean _tmp2_;
	gboolean result;
	g_return_val_if_fail (data != NULL, FALSE);
	g_return_val_if_fail (member != NULL, FALSE);
	_tmp0_ = utils_json_utils_get_object (data);
	_tmp1_ = _tmp0_;
	_tmp2_ = json_object_has_member (_tmp1_, member);
	_json_object_unref0 (_tmp1_);
	result = _tmp2_;
	return result;
}

gboolean
utils_json_utils_is_null_member (const gchar* data,
                                 const gchar* member)
{
	JsonObject* obj = NULL;
	JsonObject* _tmp0_;
	gboolean _tmp1_ = FALSE;
	JsonObject* _tmp2_;
	gboolean result;
	g_return_val_if_fail (data != NULL, FALSE);
	g_return_val_if_fail (member != NULL, FALSE);
	_tmp0_ = utils_json_utils_get_object (data);
	obj = _tmp0_;
	_tmp2_ = obj;
	if (json_object_has_member (_tmp2_, member)) {
		JsonObject* _tmp3_;
		_tmp3_ = obj;
		_tmp1_ = json_object_get_null_member (_tmp3_, member);
	} else {
		_tmp1_ = FALSE;
	}
	result = _tmp1_;
	_json_object_unref0 (obj);
	return result;
}

UtilsJsonUtils*
utils_json_utils_construct (GType object_type)
{
	UtilsJsonUtils* self = NULL;
	self = (UtilsJsonUtils*) g_type_create_instance (object_type);
	return self;
}

UtilsJsonUtils*
utils_json_utils_new (void)
{
	return utils_json_utils_construct (UTILS_TYPE_JSON_UTILS);
}

static void
utils_value_json_utils_init (GValue* value)
{
	value->data[0].v_pointer = NULL;
}

static void
utils_value_json_utils_free_value (GValue* value)
{
	if (value->data[0].v_pointer) {
		utils_json_utils_unref (value->data[0].v_pointer);
	}
}

static void
utils_value_json_utils_copy_value (const GValue* src_value,
                                   GValue* dest_value)
{
	if (src_value->data[0].v_pointer) {
		dest_value->data[0].v_pointer = utils_json_utils_ref (src_value->data[0].v_pointer);
	} else {
		dest_value->data[0].v_pointer = NULL;
	}
}

static gpointer
utils_value_json_utils_peek_pointer (const GValue* value)
{
	return value->data[0].v_pointer;
}

static gchar*
utils_value_json_utils_collect_value (GValue* value,
                                      guint n_collect_values,
                                      GTypeCValue* collect_values,
                                      guint collect_flags)
{
	if (collect_values[0].v_pointer) {
		UtilsJsonUtils * object;
		object = collect_values[0].v_pointer;
		if (object->parent_instance.g_class == NULL) {
			return g_strconcat ("invalid unclassed object pointer for value type `", G_VALUE_TYPE_NAME (value), "'", NULL);
		} else if (!g_value_type_compatible (G_TYPE_FROM_INSTANCE (object), G_VALUE_TYPE (value))) {
			return g_strconcat ("invalid object type `", g_type_name (G_TYPE_FROM_INSTANCE (object)), "' for value type `", G_VALUE_TYPE_NAME (value), "'", NULL);
		}
		value->data[0].v_pointer = utils_json_utils_ref (object);
	} else {
		value->data[0].v_pointer = NULL;
	}
	return NULL;
}

static gchar*
utils_value_json_utils_lcopy_value (const GValue* value,
                                    guint n_collect_values,
                                    GTypeCValue* collect_values,
                                    guint collect_flags)
{
	UtilsJsonUtils ** object_p;
	object_p = collect_values[0].v_pointer;
	if (!object_p) {
		return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
	}
	if (!value->data[0].v_pointer) {
		*object_p = NULL;
	} else if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
		*object_p = value->data[0].v_pointer;
	} else {
		*object_p = utils_json_utils_ref (value->data[0].v_pointer);
	}
	return NULL;
}

GParamSpec*
utils_param_spec_json_utils (const gchar* name,
                             const gchar* nick,
                             const gchar* blurb,
                             GType object_type,
                             GParamFlags flags)
{
	UtilsParamSpecJsonUtils* spec;
	g_return_val_if_fail (g_type_is_a (object_type, UTILS_TYPE_JSON_UTILS), NULL);
	spec = g_param_spec_internal (G_TYPE_PARAM_OBJECT, name, nick, blurb, flags);
	G_PARAM_SPEC (spec)->value_type = object_type;
	return G_PARAM_SPEC (spec);
}

gpointer
utils_value_get_json_utils (const GValue* value)
{
	g_return_val_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, UTILS_TYPE_JSON_UTILS), NULL);
	return value->data[0].v_pointer;
}

void
utils_value_set_json_utils (GValue* value,
                            gpointer v_object)
{
	UtilsJsonUtils * old;
	g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, UTILS_TYPE_JSON_UTILS));
	old = value->data[0].v_pointer;
	if (v_object) {
		g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, UTILS_TYPE_JSON_UTILS));
		g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (v_object), G_VALUE_TYPE (value)));
		value->data[0].v_pointer = v_object;
		utils_json_utils_ref (value->data[0].v_pointer);
	} else {
		value->data[0].v_pointer = NULL;
	}
	if (old) {
		utils_json_utils_unref (old);
	}
}

void
utils_value_take_json_utils (GValue* value,
                             gpointer v_object)
{
	UtilsJsonUtils * old;
	g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, UTILS_TYPE_JSON_UTILS));
	old = value->data[0].v_pointer;
	if (v_object) {
		g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, UTILS_TYPE_JSON_UTILS));
		g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (v_object), G_VALUE_TYPE (value)));
		value->data[0].v_pointer = v_object;
	} else {
		value->data[0].v_pointer = NULL;
	}
	if (old) {
		utils_json_utils_unref (old);
	}
}

static void
utils_json_utils_class_init (UtilsJsonUtilsClass * klass,
                             gpointer klass_data)
{
	utils_json_utils_parent_class = g_type_class_peek_parent (klass);
	((UtilsJsonUtilsClass *) klass)->finalize = utils_json_utils_finalize;
}

static void
utils_json_utils_instance_init (UtilsJsonUtils * self,
                                gpointer klass)
{
	self->ref_count = 1;
}

static void
utils_json_utils_finalize (UtilsJsonUtils * obj)
{
	UtilsJsonUtils * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, UTILS_TYPE_JSON_UTILS, UtilsJsonUtils);
	g_signal_handlers_destroy (self);
}

static GType
utils_json_utils_get_type_once (void)
{
	static const GTypeValueTable g_define_type_value_table = { utils_value_json_utils_init, utils_value_json_utils_free_value, utils_value_json_utils_copy_value, utils_value_json_utils_peek_pointer, "p", utils_value_json_utils_collect_value, "p", utils_value_json_utils_lcopy_value };
	static const GTypeInfo g_define_type_info = { sizeof (UtilsJsonUtilsClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) utils_json_utils_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (UtilsJsonUtils), 0, (GInstanceInitFunc) utils_json_utils_instance_init, &g_define_type_value_table };
	static const GTypeFundamentalInfo g_define_type_fundamental_info = { (G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE) };
	GType utils_json_utils_type_id;
	utils_json_utils_type_id = g_type_register_fundamental (g_type_fundamental_next (), "UtilsJsonUtils", &g_define_type_info, &g_define_type_fundamental_info, 0);
	return utils_json_utils_type_id;
}

GType
utils_json_utils_get_type (void)
{
	static volatile gsize utils_json_utils_type_id__once = 0;
	if (g_once_init_enter (&utils_json_utils_type_id__once)) {
		GType utils_json_utils_type_id;
		utils_json_utils_type_id = utils_json_utils_get_type_once ();
		g_once_init_leave (&utils_json_utils_type_id__once, utils_json_utils_type_id);
	}
	return utils_json_utils_type_id__once;
}

gpointer
utils_json_utils_ref (gpointer instance)
{
	UtilsJsonUtils * self;
	self = instance;
	g_atomic_int_inc (&self->ref_count);
	return instance;
}

void
utils_json_utils_unref (gpointer instance)
{
	UtilsJsonUtils * self;
	self = instance;
	if (g_atomic_int_dec_and_test (&self->ref_count)) {
		UTILS_JSON_UTILS_GET_CLASS (self)->finalize (self);
		g_type_free_instance ((GTypeInstance *) self);
	}
}

