GError*
m2v2_json_load_single_header (struct json_object *j, gpointer *pbean)
{
	GError *err = NULL;
	GByteArray *id = NULL, *hash = NULL;
	struct bean_CONTENTS_HEADERS_s *header = NULL;
	struct json_object *jid, *jhash, *jsize, *jctime, *jmtime, *jmethod, *jtype;
	struct oio_ext_json_mapping_s mapping[] = {
		{"id",     &jid,    json_type_string, 1},
		{"hash",   &jhash,  json_type_string, 1},
		{"size",   &jsize,  json_type_int, 1},
		{"ctime",  &jctime, json_type_int, 0},
		{"mtime",  &jmtime, json_type_int, 0},
		{"chunk-method", &jmethod, json_type_string, 1},
		{"mime-type",    &jtype,   json_type_string, 1},
		{NULL, NULL, 0, 0}
	};

	*pbean = NULL;
	if (NULL != (err = oio_ext_extract_json (j, mapping)))
		return err;

	id = metautils_gba_from_hexstring(json_object_get_string(jid));
	if (!id) {
		err = NEWERROR(CODE_BAD_REQUEST, "Invalid header, not hexa id");
		goto exit;
	}
	hash = metautils_gba_from_hexstring(json_object_get_string(jhash));
	if (!hash || hash->len != 16) {
		err = NEWERROR(CODE_BAD_REQUEST, "Invalid header, not hexa16 hash");
		goto exit;
	}

	header = _bean_create (&descr_struct_CONTENTS_HEADERS);
	CONTENTS_HEADERS_set2_id (header, id->data, id->len);
	CONTENTS_HEADERS_set2_hash (header, hash->data, hash->len);
	CONTENTS_HEADERS_set_size (header, json_object_get_int64(jsize));
	if (jctime)
		CONTENTS_HEADERS_set_ctime (header, json_object_get_int64(jctime));
	if (jmtime)
		CONTENTS_HEADERS_set_mtime (header, json_object_get_int64(jmtime));
	CONTENTS_HEADERS_set2_chunk_method (header, json_object_get_string(jmethod));
	CONTENTS_HEADERS_set2_mime_type (header, json_object_get_string(jtype));

	*pbean = header;
	header = NULL;

exit:
	metautils_gba_unref (id);
	metautils_gba_unref (hash);
	_bean_clean (header);
	return err;
}
GError*
m2v2_json_load_single_chunk (struct json_object *j, gpointer *pbean)
{
	GError *err = NULL;
	GByteArray *hid = NULL, *hash = NULL;
	struct bean_CHUNKS_s *chunk = NULL;
	struct json_object *jid, *jcontent, *jhash, *jsize, *jctime, *jpos;
	struct oio_ext_json_mapping_s mapping[] = {
		{"id",      &jid,      json_type_string, 1},
		{"hash",    &jhash,    json_type_string, 1},
		{"size",    &jsize,    json_type_int, 1},
		{"ctime",   &jctime,   json_type_int, 0},
		{"content", &jcontent, json_type_string, 1},
		{"pos",     &jpos,     json_type_string, 1},
		{NULL, NULL, 0, 0}
	};

	*pbean = NULL;
	if (NULL != (err = oio_ext_extract_json (j, mapping)))
		return err;

	hid = metautils_gba_from_hexstring(json_object_get_string(jid));
	if (!hid) {
		err = NEWERROR(CODE_BAD_REQUEST, "Invalid header, not hexa id");
		goto exit;
	}
	hash = metautils_gba_from_hexstring(json_object_get_string(jhash));
	if (!hash) {
		err = NEWERROR(CODE_BAD_REQUEST, "Invalid chunk, not hexa header id");
		goto exit;
	}

	chunk = _bean_create (&descr_struct_CHUNKS);
	CHUNKS_set2_id (chunk, json_object_get_string(jid));
	CHUNKS_set_hash (chunk, hash);
	CHUNKS_set_size (chunk, json_object_get_int64(jsize));
	CHUNKS_set_ctime (chunk, !jctime ? g_get_real_time() / G_TIME_SPAN_SECOND : json_object_get_int64(jctime));
	CHUNKS_set_content (chunk, hid);
	CHUNKS_set2_position (chunk, json_object_get_string (jpos));
	*pbean = chunk;
	chunk = NULL;

exit:
	metautils_gba_unref (hid);
	metautils_gba_unref (hash);
	_bean_clean (chunk);
	return err;
}
GError*
m2v2_json_load_single_alias (struct json_object *j, gpointer *pbean)
{
	GError *err = NULL;
	GByteArray *hid = NULL;
	struct bean_ALIASES_s *alias = NULL;
	struct json_object *jname, *jversion, *jctime, *jmd, *jheader;
	struct metautils_json_mapping_s m[] = {
		{"name",   &jname,    json_type_string, 1},
		{"ver",    &jversion, json_type_int,    1},
		{"ctime",  &jctime,   json_type_int,    1},
		{"header", &jheader,  json_type_string, 1},
		{"system_metadata", &jmd, json_type_string, 1},
		{NULL, NULL, 0, 0}
	};

	*pbean = NULL;
	if (NULL != (err = metautils_extract_json(j, m)))
		goto exit;

	hid = metautils_gba_from_hexstring(json_object_get_string(jheader));
	if (!hid) {
		err = NEWERROR(CODE_BAD_REQUEST, "Invalid alias, not hexadecimal header_id");
		goto exit;
	}

	alias = _bean_create (&descr_struct_ALIASES);
	ALIASES_set_deleted (alias, FALSE);
	ALIASES_set_container_version (alias, 0);
	ALIASES_set_ctime (alias, 0);
	ALIASES_set2_alias (alias, json_object_get_string(jname));
	ALIASES_set_version (alias, json_object_get_int64(jversion));
	ALIASES_set2_mdsys (alias, json_object_get_string(jmd));
	ALIASES_set2_content_id (alias, hid->data, hid->len);
	*pbean = alias;
	alias = NULL;

exit:
	metautils_gba_unref (hid);
	_bean_clean (alias);
	return err;
}
GError*
m2v2_json_load_single_chunk (struct json_object *j, gpointer *pbean)
{
	GError *err = NULL;
	GByteArray *hash = NULL;
	struct bean_CHUNKS_s *chunk = NULL;
	struct json_object *jid, *jhash, *jsize;
	struct metautils_json_mapping_s mapping[] = {
		{"id",    &jid,   json_type_string, 1},
		{"hash",  &jhash, json_type_string, 1},
		{"size",  &jsize, json_type_int, 1},
		{NULL, NULL, 0, 0}
	};

	*pbean = NULL;
	if (NULL != (err = metautils_extract_json (j, mapping)))
		return err;

	hash = metautils_gba_from_hexstring(json_object_get_string(jhash));
	if (!hash) {
		err = NEWERROR(CODE_BAD_REQUEST, "Invalid chunk, not hexa header id");
		goto exit;
	}

	chunk = _bean_create (&descr_struct_CHUNKS);
	CHUNKS_set2_id (chunk, json_object_get_string(jid));
	CHUNKS_set2_hash (chunk, hash->data, hash->len);
	CHUNKS_set_size (chunk, json_object_get_int64(jsize));
	CHUNKS_set_ctime (chunk, 0);
	*pbean = chunk;
	chunk = NULL;

exit:
	metautils_gba_unref (hash);
	_bean_clean (chunk);
	return err;
}
GError*
m2v2_json_load_single_content (struct json_object *j, gpointer *pbean)
{
	GError *err = NULL;
	GByteArray *hid = NULL;
	struct bean_CONTENTS_s *content = NULL;
	struct json_object *jhid, *jcid, *jpos;
	struct metautils_json_mapping_s mapping[] = {
		{"hdr",   &jhid, json_type_string, 1},
		{"chunk", &jcid, json_type_string, 1},
		{"pos",   &jpos, json_type_string, 1},
		{NULL, NULL, 0, 0}
	};

	*pbean = NULL;
	if (NULL != (err = metautils_extract_json (j, mapping)))
		return err;

	hid = metautils_gba_from_hexstring(json_object_get_string(jhid));
	if (!hid) {
		err = NEWERROR(CODE_BAD_REQUEST, "Invalid content, not hexa header id");
		goto exit;
	}

	content = _bean_create (&descr_struct_CONTENTS);
	CONTENTS_set2_content_id (content, hid->data, hid->len);
	CONTENTS_set2_chunk_id (content, json_object_get_string (jcid));
	CONTENTS_set2_position (content, json_object_get_string (jpos));
	*pbean = content;
	content = NULL;

exit:
	metautils_gba_unref (hid);
	_bean_clean (content);
	return err;
}