Skip to content

Commit

Permalink
fix json parsing issue using gjson api
Browse files Browse the repository at this point in the history
Signed-off-by: Nishant Bansal <[email protected]>
  • Loading branch information
NishantBansal2003 committed Jan 15, 2025
1 parent e109bd1 commit 316e3d7
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 63 deletions.
6 changes: 0 additions & 6 deletions lib/external/parson/parson.c
Original file line number Diff line number Diff line change
Expand Up @@ -2455,12 +2455,6 @@ JSON_Status json_object_set_string(JSON_Object *object, const char *name,
return status;
}

JSON_Status json_object_set_string_parson(JSON_Object *object, const char *name,
const char *string)
{
return json_object_set_string(object, name, string);
}

JSON_Status json_object_set_string_with_len(JSON_Object *object,
const char *name,
const char *string, size_t len)
Expand Down
2 changes: 0 additions & 2 deletions lib/external/parson/parson.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,6 @@ JSON_Status json_object_set_value(JSON_Object *object, const char *name,
JSON_Value *value);
JSON_Status json_object_set_string(JSON_Object *object, const char *name,
const char *string);
JSON_Status json_object_set_string_parson(JSON_Object *object, const char *name,
const char *string);
JSON_Status json_object_set_string_with_len(
JSON_Object *object, const char *name, const char *string,
size_t len); /* length shouldn't include last null character */
Expand Down
76 changes: 39 additions & 37 deletions lib/raster/json_color_out.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/*!
\file lib/raster/json_color_out.c
\brief Raster Library - Print color table in json format
(C) 2010-2024 by the GRASS Development Team
This program is free software under the GNU General Public
License (>=v2). Read the file COPYING that comes with GRASS
for details.
* \file lib/raster/json_color_out.c
*
* \brief Raster Library - Print color table in json format
*
* (C) 2010-2024 by the GRASS Development Team
*
* This program is free software under the GNU General Public
* License (>=v2). Read the file COPYING that comes with GRASS
* for details.
*
* \author Nishant Bansal
*/

#include <math.h>
Expand All @@ -17,6 +18,7 @@
#include <string.h>

#include <grass/gis.h>
#include <grass/gjson.h>
#include <grass/glocale.h>
#include <grass/parson.h>
#include <grass/raster.h>
Expand Down Expand Up @@ -98,24 +100,24 @@ static void set_color(int r, int g, int b, ColorFormat clr_frmt,
case RGB:
snprintf(color_string, sizeof(color_string), "rgb(%d, %d, %d)", r, g,
b);
json_object_set_string_parson(color_object, "rgb", color_string);
G_json_object_set_string(color_object, "rgb", color_string);
break;

case HEX:
snprintf(color_string, sizeof(color_string), "#%02X%02X%02X", r, g, b);
json_object_set_string_parson(color_object, "hex", color_string);
G_json_object_set_string(color_object, "hex", color_string);
break;

case HSV:
rgb_to_hsv(r, g, b, &h, &s, &v);
snprintf(color_string, sizeof(color_string), "hsv(%d, %d, %d)", (int)h,
(int)s, (int)v);
json_object_set_string_parson(color_object, "hsv", color_string);
G_json_object_set_string(color_object, "hsv", color_string);
break;

case TRIPLET:
snprintf(color_string, sizeof(color_string), "%d:%d:%d", r, g, b);
json_object_set_string_parson(color_object, "triplet", color_string);
G_json_object_set_string(color_object, "triplet", color_string);
break;
}
}
Expand Down Expand Up @@ -149,24 +151,24 @@ static void write_json_rule(DCELL *val, DCELL *min, DCELL *max, int r, int g,
// Update last processed values
v0 = *val, r0 = r, g0 = g, b0 = b;

JSON_Value *color_value = json_value_init_object();
JSON_Value *color_value = G_json_value_init_object();
if (color_value == NULL) {
json_value_free(root_value);
G_json_value_free(root_value);
close_file(fp);
G_fatal_error(_("Failed to initialize JSON object. Out of memory?"));
}
JSON_Object *color_object = json_object(color_value);
JSON_Object *color_object = G_json_object(color_value);

// Set the value as a percentage if requested, otherwise set it as-is
if (perc)
json_object_set_number(color_object, "value",
100 * (*val - *min) / (*max - *min));
G_json_object_set_number(color_object, "value",
100 * (*val - *min) / (*max - *min));
else
json_object_set_number(color_object, "value", *val);
G_json_object_set_number(color_object, "value", *val);

set_color(r, g, b, clr_frmt, color_object);

json_array_append_value(root_array, color_value);
G_json_array_append_value(root_array, color_value);
}

/*!
Expand All @@ -182,12 +184,12 @@ static void write_json_rule(DCELL *val, DCELL *min, DCELL *max, int r, int g,
void Rast_print_json_colors(struct Colors *colors, DCELL min, DCELL max,
FILE *fp, int perc, ColorFormat clr_frmt)
{
JSON_Value *root_value = json_value_init_array();
JSON_Value *root_value = G_json_value_init_array();
if (root_value == NULL) {
close_file(fp);
G_fatal_error(_("Failed to initialize JSON array. Out of memory?"));
}
JSON_Array *root_array = json_array(root_value);
JSON_Array *root_array = G_json_array(root_value);

if (colors->version < 0) {
/* 3.0 format */
Expand Down Expand Up @@ -232,45 +234,45 @@ void Rast_print_json_colors(struct Colors *colors, DCELL min, DCELL max,

// Get RGB color for null values and create JSON entry
Rast_get_null_value_color(&r, &g, &b, colors);
JSON_Value *nv_value = json_value_init_object();
JSON_Value *nv_value = G_json_value_init_object();
if (nv_value == NULL) {
json_value_free(root_value);
G_json_value_free(root_value);
close_file(fp);
G_fatal_error(
_("Failed to initialize JSON object. Out of memory?"));
}
JSON_Object *nv_object = json_object(nv_value);
json_object_set_string_parson(nv_object, "value", "nv");
JSON_Object *nv_object = G_json_object(nv_value);
G_json_object_set_string(nv_object, "value", "nv");
set_color(r, g, b, clr_frmt, nv_object);
json_array_append_value(root_array, nv_value);
G_json_array_append_value(root_array, nv_value);

// Get RGB color for default values and create JSON entry
Rast_get_default_color(&r, &g, &b, colors);
JSON_Value *default_value = json_value_init_object();
JSON_Value *default_value = G_json_value_init_object();
if (default_value == NULL) {
json_value_free(root_value);
G_json_value_free(root_value);
close_file(fp);
G_fatal_error(
_("Failed to initialize JSON object. Out of memory?"));
}
JSON_Object *default_object = json_object(default_value);
json_object_set_string_parson(default_object, "value", "default");
JSON_Object *default_object = G_json_object(default_value);
G_json_object_set_string(default_object, "value", "default");
set_color(r, g, b, clr_frmt, default_object);
json_array_append_value(root_array, default_value);
G_json_array_append_value(root_array, default_value);
}

// Serialize JSON array to a string and print to the file
char *json_string = json_serialize_to_string_pretty(root_value);
char *json_string = G_json_serialize_to_string_pretty(root_value);
if (!json_string) {
json_value_free(root_value);
G_json_value_free(root_value);
close_file(fp);
G_fatal_error(_("Failed to serialize JSON to pretty format."));
}

fputs(json_string, fp);

json_free_serialized_string(json_string);
json_value_free(root_value);
G_json_free_serialized_string(json_string);
G_json_value_free(root_value);

close_file(fp);
}
9 changes: 0 additions & 9 deletions raster/r.colors.out/tests/r3_colors_out_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Tests of r3.colors.out"""

import pytest
import grass.script as gs


Expand All @@ -23,23 +22,20 @@ def validate_plain_text_output(data):
), f"test failed: expected {expected} but got {data.keys()}"


@pytest.mark.needs_solo_run
def test_r3_colors_out_plain_output(raster3_color_dataset):
"""Test r3.colors.out command for plain output format."""
session = raster3_color_dataset
data = gs.parse_command("r3.colors.out", map="b", format="plain", env=session.env)
validate_plain_text_output(data)


@pytest.mark.needs_solo_run
def test_r3_colors_out_without_format_option(raster3_color_dataset):
"""Test r3.colors.out command without any format option."""
session = raster3_color_dataset
data = gs.parse_command("r3.colors.out", map="b", env=session.env)
validate_plain_text_output(data)


@pytest.mark.needs_solo_run
def test_r3_colors_out_with_p_flag(raster3_color_dataset):
"""Test r3.colors.out command with percentage values."""
session = raster3_color_dataset
Expand Down Expand Up @@ -69,7 +65,6 @@ def validate_common_json_structure(data):
), "The length of the output JSON does not match the expected value of 8."


@pytest.mark.needs_solo_run
def test_r3_colors_out_json_with_default_option(raster3_color_dataset):
"""Test r3.colors.out command for JSON output format for default color option."""
session = raster3_color_dataset
Expand All @@ -88,7 +83,6 @@ def test_r3_colors_out_json_with_default_option(raster3_color_dataset):
assert expected == data, f"test failed: expected {expected} but got {data}"


@pytest.mark.needs_solo_run
def test_r3_colors_out_json_with_triplet_option(raster3_color_dataset):
"""Test r3.colors.out command for JSON output format for triplet color option."""
session = raster3_color_dataset
Expand All @@ -109,7 +103,6 @@ def test_r3_colors_out_json_with_triplet_option(raster3_color_dataset):
assert expected == data, f"test failed: expected {expected} but got {data}"


@pytest.mark.needs_solo_run
def test_r3_colors_out_json_with_rgb_option(raster3_color_dataset):
"""Test r3.colors.out command for JSON output format for rgb color option."""
session = raster3_color_dataset
Expand All @@ -134,7 +127,6 @@ def test_r3_colors_out_json_with_rgb_option(raster3_color_dataset):
assert expected == data, f"test failed: expected {expected} but got {data}"


@pytest.mark.needs_solo_run
def test_r3_colors_out_json_with_hex_option(raster3_color_dataset):
"""Test r3.colors.out command for JSON output format for hex color option."""
session = raster3_color_dataset
Expand All @@ -159,7 +151,6 @@ def test_r3_colors_out_json_with_hex_option(raster3_color_dataset):
assert expected == data, f"test failed: expected {expected} but got {data}"


@pytest.mark.needs_solo_run
def test_r3_colors_out_json_with_hsv_option(raster3_color_dataset):
"""Test r3.colors.out command for JSON output format for hsv color option."""
session = raster3_color_dataset
Expand Down
9 changes: 0 additions & 9 deletions raster/r.colors.out/tests/r_colors_out_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Tests of r.colors.out"""

import pytest
import grass.script as gs


Expand All @@ -23,23 +22,20 @@ def validate_plain_text_output(data):
), f"test failed: expected {expected} but got {data.keys()}"


@pytest.mark.needs_solo_run
def test_r_colors_out_plain_output(raster_color_dataset):
"""Test r.colors.out command for plain output format."""
session = raster_color_dataset
data = gs.parse_command("r.colors.out", map="a", format="plain", env=session.env)
validate_plain_text_output(data)


@pytest.mark.needs_solo_run
def test_r_colors_out_without_format_option(raster_color_dataset):
"""Test r.colors.out command without any format option."""
session = raster_color_dataset
data = gs.parse_command("r.colors.out", map="a", env=session.env)
validate_plain_text_output(data)


@pytest.mark.needs_solo_run
def test_r_colors_out_with_p_flag(raster_color_dataset):
"""Test r.colors.out command with percentage values."""
session = raster_color_dataset
Expand Down Expand Up @@ -69,7 +65,6 @@ def validate_common_json_structure(data):
), "The length of the output JSON does not match the expected value of 8."


@pytest.mark.needs_solo_run
def test_r_colors_out_json_with_default_option(raster_color_dataset):
"""Test r.colors.out command for JSON output format for default color option."""
session = raster_color_dataset
Expand All @@ -88,7 +83,6 @@ def test_r_colors_out_json_with_default_option(raster_color_dataset):
assert expected == data, f"test failed: expected {expected} but got {data}"


@pytest.mark.needs_solo_run
def test_r_colors_out_json_with_triplet_option(raster_color_dataset):
"""Test r.colors.out command for JSON output format for triplet color option."""
session = raster_color_dataset
Expand All @@ -109,7 +103,6 @@ def test_r_colors_out_json_with_triplet_option(raster_color_dataset):
assert expected == data, f"test failed: expected {expected} but got {data}"


@pytest.mark.needs_solo_run
def test_r_colors_out_json_with_rgb_option(raster_color_dataset):
"""Test r.colors.out command for JSON output format for rgb color option."""
session = raster_color_dataset
Expand All @@ -134,7 +127,6 @@ def test_r_colors_out_json_with_rgb_option(raster_color_dataset):
assert expected == data, f"test failed: expected {expected} but got {data}"


@pytest.mark.needs_solo_run
def test_r_colors_out_json_with_hex_option(raster_color_dataset):
"""Test r.colors.out command for JSON output format for hex color option."""
session = raster_color_dataset
Expand All @@ -159,7 +151,6 @@ def test_r_colors_out_json_with_hex_option(raster_color_dataset):
assert expected == data, f"test failed: expected {expected} but got {data}"


@pytest.mark.needs_solo_run
def test_r_colors_out_json_with_hsv_option(raster_color_dataset):
"""Test r.colors.out command for JSON output format for hsv color option."""
session = raster_color_dataset
Expand Down

0 comments on commit 316e3d7

Please sign in to comment.