2.2.7.3.2 JSON::ToString
Version
Minimum Origin Version Required: Origin 2017 SR0
Description
Converts a struct/class object to a JSON string
Syntax
BOOL ToString( ClassObject data, string & str, OCJSONToString * params = NULL )
BOOL ToString( const vectorbase & data, string & str, OCJSONToString * params = NULL )
Parameters
- data
- [input] Reference to a struct/class/vectorbase object
- str
- [output] Stringified JSON string from given object
- params
- [input] Pointer to a struct that holds additional parameters. See struct OCJSONToString below
- data
- [input] Origin C vector to convert to Javascript array
- str
- [output] Stringified JSON string from given object
- params
- [input] Pointer to a struct that holds additional parameters. See struct OCJSONToString below
Return
TRUE on success, FALSE on failure
Examples
EX1
void ToString_ex1()
{
RECT rect = {12,34,56,78};
string strJSON;
JSON.ToString(rect, strJSON);
out_str(strJSON);
}
EX2
struct TestObj
{
float ff;
double dd;
string str;
};
void ToString_ex2()
{
TestObj obj;
obj.ff = 1.234567890123456789;
obj.dd = 1.234567890123456789;
obj.str = "3.1415926";
string strJSON1, strJSON2;
OCJSONToString params;
params.szFloatFmt = ".2"; //2 digits after decimal
JSON.ToString(obj, strJSON1);
JSON.ToString(obj, strJSON2, ¶ms);
out_str(strJSON1);
out_str(strJSON2);
}
EX3
void TestJSON_vector()
{
vector<string> vs1;
vs1.Add("hello1");
vs1.Add("hello2");
vs1.Add("hello3");
string strvs1;
OCJSONToString param;
param.dwFlags = JSONToStringFlagsDisableIndentation;
JSON.ToString(vs1, strvs1, ¶m);
string strvd1;
vector<double> vd1;
vd1.Add(3.14);
vd1.Add(1024);
vd1.Add(-3687.3645);
JSON.ToString(vd1, strvd1, ¶m);
string strvs2 = "[\"hello1\",\"hello2\",\"hello3\"]";
vector<string> vs2;
JSON.FromString(vs2, strvs2);
string strvd2 = "[3.14, 1024, -3687.3645]";
vector<double> vd2;
JSON.FromString(vd2, strvd2);
Worksheet wks;
wks.Create("Origin");
Dataset dsA(wks, 0);
string strds1 = "[314.56, -963, 6317, 8]";
JSON.FromString(dsA, strds1);
string strds2;
JSON.ToString(dsA, strds2);
}
EX4
- (Origin2026)Convert Tree to JSON
void Tree_to_JSON_ex()
{
//construct a tree
Tree tr;
tr.a.nVal = 123;
//tr.a.sub.nVal = 2222;
tr.b.strVal = "Hello";
tr.c.dVal = 3.14;
tr.a.SetAttribute("test1", 789);
tr.a.SetAttribute("abc", "what");
vector v1 = {1.23, 3.45, 6.78};
tr.a.SetAttribute("v1", v1);
vector vTemp1;
tr.a.GetAttribute("v1", vTemp1);
vector<int> vsTemp1;
tr.a.GetAttribute("v1", vsTemp1);
tr.d.test1.dVals = v1;
tr.d.test2.SetAttribute("junk", 3.14);
tr.d.test2.SetAttribute("arr", 4.79);
tr.d.test2.SetAttribute("p1", (__int64)0x12345678abcdef01);
tr.d.test2.SetAttribute("p2", (void*)&vsTemp1);
vector<string> vs = {"tt1", "ok", "damn"};
tr.a.SetAttribute("vs", vs);
tr.d.test3.strVals = vs;
matrix<double> mat;
mat.SetSize(3, 4);
for (int nRow = 0; nRow < 3; ++nRow)
{
for (int nCol = 0; nCol < 4; ++nCol)
mat[nRow][nCol] = nRow+nCol * 6;
}
MatrixPage mp;
mp.Create("origin");
MatrixLayer ml = mp.Layers(0);
Matrix Mat(ml);
Mat = mat;
int nCols = mat.GetNumCols();
tr.d.test4.dVals2 = mat;
matrix<double> mat2;
mat2 = tr.d.test4.dVals2;
string str;
tr.GetString(str);
//out_str(str);
out_tree(tr);
//Tree to JSON
string str2;
OCJSONToString params;
params.dwFlags = JSONToStringFlagsConvertXMLElementsAttributes;
JSON.ToString(tr, str2, ¶ms);
//out_str(str2);
//JSON to Tree
Tree tr2;
OCJSONFromString params2;
params2.dwLoad = JSONLOAD_CONVERT_XML_ELEMENTS_ATTRIBUTES;
JSON.FromString(tr2, str2, ¶ms2);
out_tree(tr2);
}
Remark
Declaration of OCJSONToString
enum JSONToStringFlags {
JSONToStringFlagsDisableIndentation = 0x00000001,
JSONToStringFlagsConvertXMLElementsAttributes = 0x00000002, //Origin2026's new bit, parse the XML and try to convert the elements and attributes into JSON object rather than a simple XML string.
};
typedef struct OCJSONToString
{
const char* szFloatFmt;
DWORD dwFlags;
}OCJSONToString;
szFloatFmt
- LabTalk formatting string. For a complete list of formatting options, please see the Origin Formats.
Always use dot as decimal point
- use ftoaAlwaysUseDotAsDecimalPoint class with braces( so that it will not affect subsequent codes )
OCJSONToString params;
params.szFloatFmt = ".2"; // 2 digits after dot decimal sep.
string str;
{
ftoaAlwaysUseDotAsDecimalPoint alwaysUseDot;
JSON.ToString(st, str, ¶ms);
}
See Also
JSON::FromString
Header to Included
origin.h
|