Commit cafbe895 authored by Gabriel Ivascu's avatar Gabriel Ivascu

sync-records: Convert NULL to empty string for serializable string properties

Firefox may afford to work with NULL values since JavaScript is more
permissive than C, but we cannot afford to crash because of a random
misplaced NULL value. Better be safe than sorry.
parent 0381b34e
......@@ -307,16 +307,18 @@ serializable_serialize_property (JsonSerializable *serializable,
const GValue *value,
GParamSpec *pspec)
{
if (G_VALUE_HOLDS_STRING (value) && g_value_get_string (value) == NULL) {
JsonNode *node = json_node_new (JSON_NODE_VALUE);
json_node_set_string (node, "");
return node;
}
if (!g_strcmp0 (name, "visits")) {
JsonNode *node;
JsonArray *array;
GSequence *visits;
JsonNode *node = json_node_new (JSON_NODE_ARRAY);
JsonArray *array = json_array_new ();
GSequence *visits = g_value_get_pointer (value);
GSequenceIter *it;
node = json_node_new (JSON_NODE_ARRAY);
array = json_array_new ();
visits = g_value_get_pointer (value);
if (visits != NULL) {
for (it = g_sequence_get_begin_iter (visits); !g_sequence_iter_is_end (it); it = g_sequence_iter_next (it)) {
EphyHistoryRecordVisit *visit = g_sequence_get (it);
......@@ -342,12 +344,14 @@ serializable_deserialize_property (JsonSerializable *serializable,
GParamSpec *pspec,
JsonNode *node)
{
if (!g_strcmp0 (name, "visits")) {
JsonArray *array;
GSequence *visits;
if (G_VALUE_HOLDS_STRING (value) && JSON_NODE_HOLDS_NULL (node)) {
g_value_set_string (value, "");
return TRUE;
}
array = json_node_get_array (node);
visits = g_sequence_new ((GDestroyNotify)ephy_history_record_visit_free);
if (!g_strcmp0 (name, "visits")) {
JsonArray *array = json_node_get_array (node);
GSequence *visits = g_sequence_new ((GDestroyNotify)ephy_history_record_visit_free);
for (guint i = 0; i < json_array_get_length (array); i++) {
JsonObject *object = json_node_get_object (json_array_get_element (array, i));
......
......@@ -220,6 +220,12 @@ serializable_serialize_property (JsonSerializable *serializable,
const GValue *value,
GParamSpec *pspec)
{
if (G_VALUE_HOLDS_STRING (value) && g_value_get_string (value) == NULL) {
JsonNode *node = json_node_new (JSON_NODE_VALUE);
json_node_set_string (node, "");
return node;
}
if (!g_strcmp0 (name, "tabs")) {
JsonNode *node = json_node_new (JSON_NODE_ARRAY);
JsonArray *array = json_array_new ();
......@@ -242,6 +248,11 @@ serializable_deserialize_property (JsonSerializable *serializable,
GParamSpec *pspec,
JsonNode *node)
{
if (G_VALUE_HOLDS_STRING (value) && JSON_NODE_HOLDS_NULL (node)) {
g_value_set_string (value, "");
return TRUE;
}
if (!g_strcmp0 (name, "tabs")) {
JsonArray *array;
GList *tabs = NULL;
......
......@@ -520,16 +520,17 @@ serializable_serialize_property (JsonSerializable *serializable,
const GValue *value,
GParamSpec *pspec)
{
JsonNode *node = NULL;
if (G_VALUE_HOLDS_STRING (value) && g_value_get_string (value) == NULL) {
JsonNode *node = json_node_new (JSON_NODE_VALUE);
json_node_set_string (node, "");
return node;
}
if (g_strcmp0 (name, "tags") == 0) {
GSequence *tags;
JsonNode *node = json_node_new (JSON_NODE_ARRAY);
JsonArray *array = json_array_new ();
GSequence *tags = g_value_get_pointer (value);
GSequenceIter *iter;
JsonArray *array;
node = json_node_new (JSON_NODE_ARRAY);
array = json_array_new ();
tags = g_value_get_pointer (value);
if (tags != NULL) {
for (iter = g_sequence_get_begin_iter (tags);
......@@ -540,13 +541,15 @@ serializable_serialize_property (JsonSerializable *serializable,
}
json_node_set_array (node, array);
} else if (!g_strcmp0 (name, "time-added")) {
/* This is not a Firefox bookmark property, skip it. */
} else {
node = json_serializable_default_serialize_property (serializable, name, value, pspec);
return node;
}
return node;
/* This is not a Firefox bookmark property, skip it. */
if (!g_strcmp0 (name, "time-added"))
return NULL;
return json_serializable_default_serialize_property (serializable, name, value, pspec);
}
static gboolean
......@@ -556,15 +559,16 @@ serializable_deserialize_property (JsonSerializable *serializable,
GParamSpec *pspec,
JsonNode *node)
{
if (G_VALUE_HOLDS_STRING (value) && JSON_NODE_HOLDS_NULL (node)) {
g_value_set_string (value, "");
return TRUE;
}
if (g_strcmp0 (name, "tags") == 0) {
GSequence *tags;
JsonArray *array;
GSequence *tags = g_sequence_new (g_free);
JsonArray *array = json_node_get_array (node);
const char *tag;
g_assert (JSON_NODE_HOLDS_ARRAY (node));
array = json_node_get_array (node);
tags = g_sequence_new (g_free);
for (gsize i = 0; i < json_array_get_length (array); i++) {
tag = json_node_get_string (json_array_get_element (array, i));
g_sequence_insert_sorted (tags, g_strdup (tag),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment