Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rely on text internals to render json #35

Merged

Conversation

solidsnack
Copy link
Collaborator

The output function lookup will be very hard because I am not sure how even to
find the type OID for json (not sure how to get a type from a Datum).

Under the hood, json (but not jsonb) is text. You can see one consequence
of this in the bodies of textout and json_out:

Datum
textout(PG_FUNCTION_ARGS)
{
    Datum       txt = PG_GETARG_DATUM(0);

    PG_RETURN_CSTRING(TextDatumGetCString(txt));
}

Datum
json_out(PG_FUNCTION_ARGS)
{
    /* we needn't detoast because text_to_cstring will handle that */
    Datum       txt = PG_GETARG_DATUM(0);

    PG_RETURN_CSTRING(TextDatumGetCString(txt));
}

They are the same function. Clicking through the macros we get a cast and then
the conversion to a CString with text_to_cstring. So we don't need an ouput
function after all.

The output function lookup will be very hard because I am not sure how even to
find the type OID for `json` (not sure how to get a type from a Datum).

Under the hood, `json` (but not `jsonb`) is text. You can see one consequence
of this in the bodies of [`textout`] and [`json_out`]:

    Datum
    textout(PG_FUNCTION_ARGS)
    {
        Datum       txt = PG_GETARG_DATUM(0);

        PG_RETURN_CSTRING(TextDatumGetCString(txt));
    }

    Datum
    json_out(PG_FUNCTION_ARGS)
    {
        /* we needn't detoast because text_to_cstring will handle that */
        Datum       txt = PG_GETARG_DATUM(0);

        PG_RETURN_CSTRING(TextDatumGetCString(txt));
    }

They are the same function. Clicking through the macros we get a cast and then
the conversion to a CString with `text_to_cstring`. So we don't need an ouput
function after all...

[`textout`]: http://doxygen.postgresql.org/varlena_8c_source.html#l00520
[`json_out`]: http://doxygen.postgresql.org/json_8c_source.html#l00245
solidsnack added a commit that referenced this pull request Feb 10, 2016
@solidsnack solidsnack merged commit 40981c7 into solidsnack-cleanup-rust-build Feb 10, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant