static void do_geojson_test(char * in, char * out, char * srs, int precision, int has_bbox)
{
    LWGEOM *g;
    char * h;

    g = lwgeom_from_wkt(in, LW_PARSER_CHECK_NONE);
    h = lwgeom_to_geojson(g, srs, precision, has_bbox);

    if (strcmp(h, out))
        fprintf(stderr, "\nIn:   %s\nOut:  %s\nTheo: %s\n", in, h, out);

    CU_ASSERT_STRING_EQUAL(h, out);

    lwgeom_free(g);
    lwfree(h);
}
static void do_geojson_unsupported(char * in, char * out)
{
    LWGEOM *g;
    char *h;

    g = lwgeom_from_wkt(in, LW_PARSER_CHECK_NONE);
    h = lwgeom_to_geojson(g, NULL, 0, 0);

    if (strcmp(cu_error_msg, out))
        fprintf(stderr, "\nIn:   %s\nOut:  %s\nTheo: %s\n",
                in, cu_error_msg, out);

    CU_ASSERT_STRING_EQUAL(out, cu_error_msg);
    cu_error_msg_reset();

    lwfree(h);
    lwgeom_free(g);
}
Datum geography_as_geojson(PG_FUNCTION_ARGS)
{
	LWGEOM *lwgeom = NULL;
	GSERIALIZED *g = NULL;
	char *geojson;
	text *result;
	int version;
	int option = 0;
	int has_bbox = 0;
	int precision = OUT_MAX_DOUBLE_PRECISION;
	char * srs = NULL;

	/* Get the version */
	version = PG_GETARG_INT32(0);
	if ( version != 1)
	{
		elog(ERROR, "Only GeoJSON 1 is supported");
		PG_RETURN_NULL();
	}

	/* Get the geography */
	if (PG_ARGISNULL(1) ) PG_RETURN_NULL();
	g = (GSERIALIZED*)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));

	/* Convert to lwgeom so we can run the old functions */
	lwgeom = lwgeom_from_gserialized(g);

	/* Retrieve precision if any (default is max) */
	if (PG_NARGS() >2 && !PG_ARGISNULL(2))
	{
		precision = PG_GETARG_INT32(2);
		if ( precision > OUT_MAX_DOUBLE_PRECISION )
			precision = OUT_MAX_DOUBLE_PRECISION;
		else if ( precision < 0 ) precision = 0;
	}

	/* Retrieve output option
	 * 0 = without option (default)
	 * 1 = bbox
	 * 2 = short crs
	 * 4 = long crs
	 */
	if (PG_NARGS() >3 && !PG_ARGISNULL(3))
		option = PG_GETARG_INT32(3);

	if (option & 2 || option & 4)
	{
		/* Geography only handle srid SRID_DEFAULT */
		if (option & 2) srs = getSRSbySRID(SRID_DEFAULT, true);
		if (option & 4) srs = getSRSbySRID(SRID_DEFAULT, false);

		if (!srs)
		{
			elog(ERROR, "SRID SRID_DEFAULT unknown in spatial_ref_sys table");
			PG_RETURN_NULL();
		}
	}

	if (option & 1) has_bbox = 1;

	geojson = lwgeom_to_geojson(lwgeom, srs, precision, has_bbox);
    lwgeom_free(lwgeom);
	PG_FREE_IF_COPY(g, 1);
	if (srs) pfree(srs);

	result = cstring2text(geojson);
	lwfree(geojson);

	PG_RETURN_TEXT_P(result);
}