| 
									
										
										
										
											2015-01-22 15:01:37 +01:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2016-05-04 18:49:17 +02:00
										 |  |  |  * A simple JSON writer | 
					
						
							| 
									
										
										
										
											2015-01-22 15:01:37 +01:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Copyright Alexander Graf | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Authors: | 
					
						
							| 
									
										
										
										
											2015-02-07 11:25:50 +01:00
										 |  |  |  *  Alexander Graf <agraf@suse.de> | 
					
						
							| 
									
										
										
										
											2015-01-22 15:01:37 +01:00
										 |  |  |  * | 
					
						
							|  |  |  |  * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. | 
					
						
							|  |  |  |  * See the COPYING.LIB file in the top-level directory. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-04 18:49:17 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Type QJSON lets you build JSON text.  Its interface mirrors (a | 
					
						
							|  |  |  |  * subset of) abstract JSON syntax. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * It does *not* detect incorrect use.  It happily produces invalid | 
					
						
							|  |  |  |  * JSON then.  This is what migration wants. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * QAPI output visitors also produce JSON text.  However, they do | 
					
						
							|  |  |  |  * assert their preconditions and invariants, and therefore abort on | 
					
						
							|  |  |  |  * incorrect use. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-29 17:50:05 +00:00
										 |  |  | #include "qemu/osdep.h"
 | 
					
						
							| 
									
										
										
										
											2016-05-04 18:49:17 +02:00
										 |  |  | #include "qapi/qmp/qstring.h"
 | 
					
						
							| 
									
										
										
										
											2017-04-20 13:10:28 +02:00
										 |  |  | #include "qjson.h"
 | 
					
						
							| 
									
										
										
										
											2015-01-22 15:01:37 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | struct QJSON { | 
					
						
							|  |  |  |     QString *str; | 
					
						
							|  |  |  |     bool omit_comma; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void json_emit_element(QJSON *json, const char *name) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /* Check whether we need to print a , before an element */ | 
					
						
							|  |  |  |     if (json->omit_comma) { | 
					
						
							|  |  |  |         json->omit_comma = false; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         qstring_append(json->str, ", "); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (name) { | 
					
						
							|  |  |  |         qstring_append(json->str, "\""); | 
					
						
							|  |  |  |         qstring_append(json->str, name); | 
					
						
							|  |  |  |         qstring_append(json->str, "\" : "); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void json_start_object(QJSON *json, const char *name) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     json_emit_element(json, name); | 
					
						
							|  |  |  |     qstring_append(json->str, "{ "); | 
					
						
							|  |  |  |     json->omit_comma = true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void json_end_object(QJSON *json) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     qstring_append(json->str, " }"); | 
					
						
							|  |  |  |     json->omit_comma = false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void json_start_array(QJSON *json, const char *name) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     json_emit_element(json, name); | 
					
						
							|  |  |  |     qstring_append(json->str, "[ "); | 
					
						
							|  |  |  |     json->omit_comma = true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void json_end_array(QJSON *json) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     qstring_append(json->str, " ]"); | 
					
						
							|  |  |  |     json->omit_comma = false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void json_prop_int(QJSON *json, const char *name, int64_t val) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     json_emit_element(json, name); | 
					
						
							|  |  |  |     qstring_append_int(json->str, val); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void json_prop_str(QJSON *json, const char *name, const char *str) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     json_emit_element(json, name); | 
					
						
							|  |  |  |     qstring_append_chr(json->str, '"'); | 
					
						
							|  |  |  |     qstring_append(json->str, str); | 
					
						
							|  |  |  |     qstring_append_chr(json->str, '"'); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const char *qjson_get_str(QJSON *json) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return qstring_get_str(json->str); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | QJSON *qjson_new(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-05-04 18:49:18 +02:00
										 |  |  |     QJSON *json = g_new0(QJSON, 1); | 
					
						
							| 
									
										
										
										
											2015-01-22 15:01:37 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     json->str = qstring_from_str("{ "); | 
					
						
							|  |  |  |     json->omit_comma = true; | 
					
						
							| 
									
										
										
										
											2016-05-04 18:49:18 +02:00
										 |  |  |     return json; | 
					
						
							| 
									
										
										
										
											2015-01-22 15:01:37 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-04 18:49:18 +02:00
										 |  |  | void qjson_finish(QJSON *json) | 
					
						
							| 
									
										
										
										
											2015-01-22 15:01:37 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-05-04 18:49:18 +02:00
										 |  |  |     json_end_object(json); | 
					
						
							| 
									
										
										
										
											2015-01-22 15:01:37 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-04 18:49:18 +02:00
										 |  |  | void qjson_destroy(QJSON *json) | 
					
						
							| 
									
										
										
										
											2015-01-22 15:01:37 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-07-15 10:41:03 +02:00
										 |  |  |     QDECREF(json->str); | 
					
						
							| 
									
										
										
										
											2016-05-04 18:49:18 +02:00
										 |  |  |     g_free(json); | 
					
						
							| 
									
										
										
										
											2015-01-22 15:01:37 +01:00
										 |  |  | } |