Browse Source

Add an option to add an EOL marker to json_dumps

master
Con Kolivas 10 years ago
parent
commit
906d24a554
  1. 9
      src/jansson-2.6/src/dump.c
  2. 1
      src/jansson-2.6/src/jansson.h
  3. 2
      src/jansson-2.6/src/jansson_private.h
  4. 20
      src/jansson-2.6/src/memory.c

9
src/jansson-2.6/src/dump.c

@ -427,6 +427,13 @@ static int do_dump(const json_t *json, size_t flags, int depth,
} }
} }
char *json_dump_dup(const char *str, size_t flags)
{
if (flags & JSON_EOL)
return jsonp_eolstrdup(str);
return jsonp_strdup(str);
}
char *json_dumps(const json_t *json, size_t flags) char *json_dumps(const json_t *json, size_t flags)
{ {
strbuffer_t strbuff; strbuffer_t strbuff;
@ -438,7 +445,7 @@ char *json_dumps(const json_t *json, size_t flags)
if(json_dump_callback(json, dump_to_strbuffer, (void *)&strbuff, flags)) if(json_dump_callback(json, dump_to_strbuffer, (void *)&strbuff, flags))
result = NULL; result = NULL;
else else
result = jsonp_strdup(strbuffer_value(&strbuff)); result = json_dump_dup(strbuffer_value(&strbuff), flags);
strbuffer_close(&strbuff); strbuffer_close(&strbuff);
return result; return result;

1
src/jansson-2.6/src/jansson.h

@ -261,6 +261,7 @@ json_t *json_load_callback(json_load_callback_t callback, void *data, size_t fla
#define JSON_ENCODE_ANY 0x200 #define JSON_ENCODE_ANY 0x200
#define JSON_ESCAPE_SLASH 0x400 #define JSON_ESCAPE_SLASH 0x400
#define JSON_NO_UTF8 0x800 #define JSON_NO_UTF8 0x800
#define JSON_EOL 0x1000
typedef int (*json_dump_callback_t)(const char *buffer, size_t size, void *data); typedef int (*json_dump_callback_t)(const char *buffer, size_t size, void *data);

2
src/jansson-2.6/src/jansson_private.h

@ -1,5 +1,6 @@
/* /*
* Copyright (c) 2009-2013 Petri Lehtinen <petri@digip.org> * Copyright (c) 2009-2013 Petri Lehtinen <petri@digip.org>
* Copyright (c) 2015 Con Kolivas <kernel@kolivas.org>
* *
* Jansson is free software; you can redistribute it and/or modify * Jansson is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details. * it under the terms of the MIT license. See LICENSE for details.
@ -83,6 +84,7 @@ void* jsonp_malloc(size_t size);
void jsonp_free(void *ptr); void jsonp_free(void *ptr);
char *jsonp_strndup(const char *str, size_t length); char *jsonp_strndup(const char *str, size_t length);
char *jsonp_strdup(const char *str); char *jsonp_strdup(const char *str);
char *jsonp_eolstrdup(const char *str);
/* Windows compatibility */ /* Windows compatibility */
#ifdef _WIN32 #ifdef _WIN32

20
src/jansson-2.6/src/memory.c

@ -1,6 +1,7 @@
/* /*
* Copyright (c) 2009-2013 Petri Lehtinen <petri@digip.org> * Copyright (c) 2009-2013 Petri Lehtinen <petri@digip.org>
* Copyright (c) 2011-2012 Basile Starynkevitch <basile@starynkevitch.net> * Copyright (c) 2011-2012 Basile Starynkevitch <basile@starynkevitch.net>
* Copyright (c) 2015 Con Kolivas <kernel@kolivas.org>
* *
* Jansson is free software; you can redistribute it and/or modify it * Jansson is free software; you can redistribute it and/or modify it
* under the terms of the MIT license. See LICENSE for details. * under the terms of the MIT license. See LICENSE for details.
@ -49,6 +50,25 @@ char *jsonp_strdup(const char *str)
return new_str; return new_str;
} }
char *jsonp_eolstrdup(const char *str)
{
char *new_str;
size_t len;
len = strlen(str);
if(len == (size_t)-1)
return NULL;
new_str = jsonp_malloc(len + 2);
if(!new_str)
return NULL;
memcpy(new_str, str, len);
new_str[len] = '\n';
new_str[len + 1] = '\0';
return new_str;
}
void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn) void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn)
{ {
do_malloc = malloc_fn; do_malloc = malloc_fn;

Loading…
Cancel
Save