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, &params);
    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, &param);
	string strvd1;
	vector<double> vd1;
	vd1.Add(3.14);
	vd1.Add(1024);
	vd1.Add(-3687.3645);
	JSON.ToString(vd1, strvd1, &param);
	
	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);
}

Remark

Declaration of OCJSONToString

enum JSONToStringFlags {
	JSONToStringFlagsDisableIndentation = 0x00000001,
};

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, &params);
}

See Also

JSON::FromString

Header to Included

origin.h