[ovs-dev] [bug15637 1/5] json: New function json_serialized_length().

Justin Pettit jpettit at nicira.com
Mon Apr 1 16:41:19 UTC 2013


On Mar 27, 2013, at 2:44 PM, Ben Pfaff <blp at nicira.com> wrote:

> +static size_t
> +json_string_serialized_length(const char *string)
> +{
> +    size_t length;
> +    uint8_t c;
> +
> +    /* "" */
> +    length = 2;

I think this length represents the surrounding quotes, but until I looked at json_serialize_string(), I thought it was for just a single quote based on this comment.

> +/* Returns strlen(json_to_string(json, 0)).
> + *
> + * JSSF_SORT does not affect the length of json_to_string()'s output, but
> + * JSSF_PRETTY does. */

Is it worth mentioning that this is treating it as not pretty?

> +size_t
> +json_serialized_length(const struct json *json)
> +{
> +    switch (json->type) {
> +    case JSON_NULL:
> +        return strlen("null");
> +        break;
> +
> +    case JSON_FALSE:
> +        return strlen("false");
> +        break;
> +
> +    case JSON_TRUE:
> +        return strlen("true");
> +        break;
> +
> +    case JSON_OBJECT:
> +        return json_object_serialized_length(json->u.object);
> +
> +    case JSON_ARRAY:
> +        return json_array_serialized_length(&json->u.array);
> +
> +    case JSON_INTEGER:
> +        return snprintf(NULL, 0, "%lld", json->u.integer);
> +
> +    case JSON_REAL:
> +        return snprintf(NULL, 0, "%.*g", DBL_DIG, json->u.real);
> +
> +    case JSON_STRING:
> +        return json_string_serialized_length(json->u.string);
> +
> +    case JSON_N_TYPES:
> +    default:
> +        NOT_REACHED();
> +    }
> +}

Is there a reason for the inconsistency with some case blocks have a "break" and others not?  The json_serialize() function uses "break" in all of them.

--Justin





More information about the dev mailing list