Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » U++ community news and announcements » JSON support
JSON support [message #34240] Sat, 05 November 2011 18:15 Go to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
I guess better late than sorry. It is true that parsing JSON is trivial, at least as long as we have CParser... But now we have "official" suport in Core, with a nice reference example

#include "Core/Core.h"

using namespace Upp;

CONSOLE_APP_MAIN
{
	Value js = ParseJSON(LoadFile(GetDataFile("test.json")));
	DUMP(js);
	DUMP(js["age"]);
	Value phone_number = js["phoneNumber"];
	DUMP(phone_number);
	DUMP(phone_number.GetCount());
	for(int i = 0; i < phone_number.GetCount(); i++) {
		DUMP(phone_number[i]["type"]);
		DUMP(phone_number[i]["number"]);
	}

	DUMP(AsJSON(js, true));
	DUMP(AsJSON(js));

	LOG("- Partial parsing");
	ValueArray va;
	va << js << js << js;
	String s = AsJSON(va);
	DUMP(s);
	CParser p2(s);
	try {
		p2.PassChar('[');
		if(!p2.Char(']')) {
			do {
				DUMP(ParseJSON(p2)["firstName"]);
			}
			while(p2.Char(','));
			p2.PassChar(']');
		}
	}
	catch(CParser::Error) {
		LOG("invalid JSON");
	}
	
	LOG("- Composing JSON using support classes");
	Json json;
	json
		("firstName", "Andrew")
		("secondName", "Smith")
		("age", 28)
		("address", Json("streetAddress", "23 3rd Street")("city", "New York"))
	;
	JsonArray pn;
	pn << Json("type", "home")("number", "12312345")
	   << Json("type", "work")("number", "87126388");
	json("phoneNumber", pn);
	DUMP(~json);
	DUMP(AsJSON(ParseJSON(~json), true));
}


EDITED: added try/catch for partial parsin (ParseJSON(CParser) can throw exception).

[Updated on: Mon, 07 November 2011 15:09]

Report message to a moderator

Re: JSON support [message #34270 is a reply to message #34240] Mon, 07 November 2011 14:24 Go to previous message
ratah is currently offline  ratah
Messages: 107
Registered: July 2010
Experienced Member
Thank you for this support
it's helpfull
Previous Topic: New Value features
Next Topic: CParser now optionally supports \u (from JavaScript)
Goto Forum:
  


Current Time: Fri Apr 26 06:43:37 CEST 2024

Total time taken to generate the page: 0.02374 seconds