Skip to content

Instantly share code, notes, and snippets.

@abicky
Created January 3, 2012 19:09
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save abicky/1556388 to your computer and use it in GitHub Desktop.
patch for RJSONIO_0.96-0
diff -cr RJSONIO.orig/src/libjson/JSONOptions.h RJSONIO/src/libjson/JSONOptions.h
*** RJSONIO.orig/src/libjson/JSONOptions.h 2011-10-23 02:00:32.000000000 +0900
--- RJSONIO/src/libjson/JSONOptions.h 2012-01-05 01:54:00.000000000 +0900
***************
*** 79,85 ****
* means that libjson supports the full array of unicode characters, but also takes
* much more memory and processing power.
*/
! //#define JSON_UNICODE
/*
--- 79,85 ----
* means that libjson supports the full array of unicode characters, but also takes
* much more memory and processing power.
*/
! #define JSON_UNICODE
/*
diff -cr RJSONIO.orig/src/rlibjson.c RJSONIO/src/rlibjson.c
*** RJSONIO.orig/src/rlibjson.c 2011-10-23 02:00:32.000000000 +0900
--- RJSONIO/src/rlibjson.c 2012-01-05 01:54:03.000000000 +0900
***************
*** 1,4 ****
--- 1,6 ----
#include <libjson/libjson.h>
+ #include <stdlib.h>
+ #include <limits.h>
#include <Rdefines.h>
SEXP processJSONNode(JSONNODE *node, int parentType, int simplify, SEXP nullValue, int simplifyWithNames, cetype_t);
***************
*** 14,20 ****
--- 16,35 ----
const char * str = CHAR(STRING_ELT(r_str, 0));
JSONNODE *node;
SEXP ans;
+ #ifdef JSON_UNICODE
+ wchar_t *wstr;
+ int len = strlen(str);
+ wstr = (wchar_t *)malloc(sizeof(wchar_t) * (len + 1));
+ if (wstr == NULL) {
+ PROBLEM "Cannot allocate memory"
+ ERROR;
+ }
+ mbstowcs(wstr, str, len);
+ node = json_parse(wstr);
+ free(wstr);
+ #else
node = json_parse(str);
+ #endif
ans = processJSONNode(node, json_type(node), INTEGER(simplify)[0], nullValue, LOGICAL(simplifyWithNames)[0],
INTEGER(encoding)[0]);
json_delete(node);
***************
*** 58,64 ****
json_char *node_name = json_name(i);
! char type = json_type(i);
if(startType == 127)
startType = type;
--- 73,79 ----
json_char *node_name = json_name(i);
! json_char type = json_type(i);
if(startType == 127)
startType = type;
***************
*** 95,101 ****
--- 110,129 ----
case JSON_STRING:
{
//XXX Garbage collection
+ #ifdef JSON_UNICODE
+ wchar_t *wtmp = json_as_string(i);
+ char *tmp;
+ int len = wcslen(wtmp);
+ int size = sizeof(char) * (len * MB_LEN_MAX + 1);
+ tmp = (char *)malloc(size);
+ if (tmp == NULL) {
+ PROBLEM "Cannot allocate memory"
+ ERROR;
+ }
+ wcstombs(tmp, wtmp, size);
+ #else
char *tmp = json_as_string(i);
+ #endif
// do we need to strdup here?
#if 0
el = ScalarString(mkChar(tmp));
***************
*** 103,109 ****
--- 131,142 ----
el = ScalarString(mkCharCE(tmp, charEncoding));
#endif
elType = setType(elType, STRSXP);
+ #ifdef JSON_UNICODE
+ free(tmp);
+ json_free(wtmp);
+ #else
json_free(tmp);
+ #endif
numStrings++;
}
break;
***************
*** 119,126 ****
if(names == NULL) {
PROTECT(names = NEW_CHARACTER(len)); nprotect++;
}
! if(node_name && node_name[0])
! SET_STRING_ELT(names, ctr, mkChar(node_name));
}
json_free(node_name);
ctr++;
--- 152,174 ----
if(names == NULL) {
PROTECT(names = NEW_CHARACTER(len)); nprotect++;
}
! if(node_name && node_name[0]) {
! #ifdef JSON_UNICODE
! char *mb_node_name;
! int len = wcslen(node_name);
! int size = sizeof(char) * (len * MB_LEN_MAX + 1);
! mb_node_name = (char *)malloc(size);
! if (mb_node_name == NULL) {
! PROBLEM "Cannot allocate memory"
! ERROR;
! }
! wcstombs(mb_node_name, node_name, size);
! SET_STRING_ELT(names, ctr, mkChar(mb_node_name));
! free(mb_node_name);
! #else
! SET_STRING_ELT(names, ctr, mkChar(node_name));
! #endif
! }
}
json_free(node_name);
ctr++;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment