1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2000-2015 Ericsson Telecom AB
3 // All rights reserved. This program and the accompanying materials
4 // are made available under the terms of the Eclipse Public License v1.0
5 // which accompanies this distribution, and is available at
6 // http://www.eclipse.org/legal/epl-v10.html
7 ///////////////////////////////////////////////////////////////////////////////
9 #include "Ttcn2Json.hh"
14 #include "../../common/JSON_Tokenizer.hh"
16 // forward declarations
23 Ttcn2Json::Ttcn2Json(Common::Modules
* p_modules
, const char* p_schema_name
)
27 FILE* file
= open_output_file(p_schema_name
, &is_temporary
);
29 JSON_Tokenizer
json(true);
33 fprintf(file
, "%s\n", json
.get_buffer());
35 close_output_file(p_schema_name
, file
, is_temporary
, 0);
38 void Ttcn2Json::create_schema(JSON_Tokenizer
& json
)
40 // top-level object start
41 json
.put_next_token(JSON_TOKEN_OBJECT_START
, NULL
);
43 // insert the schema header
44 json
.put_next_token(JSON_TOKEN_NAME
, "$schema");
45 json
.put_next_token(JSON_TOKEN_STRING
, "\"http://json-schema.org/draft-04/schema#\"");
47 // start of type definitions
48 json
.put_next_token(JSON_TOKEN_NAME
, "definitions");
49 json
.put_next_token(JSON_TOKEN_OBJECT_START
, NULL
);
51 // insert module names and schemas for types; gather references to types and
52 // JSON encoding/decoding function information
53 map
<Common::Type
*, JSON_Tokenizer
> json_refs
;
54 modules
->generate_json_schema(json
, json_refs
);
56 // end of type definitions
57 json
.put_next_token(JSON_TOKEN_OBJECT_END
, NULL
);
59 if (!json_refs
.empty()) {
60 // top-level "anyOf" structure containing references to the types the schema validates
61 // don't insert an empty "anyOf" if there are no references
62 json
.put_next_token(JSON_TOKEN_NAME
, "anyOf");
63 json
.put_next_token(JSON_TOKEN_ARRAY_START
, NULL
);
65 // close schema segments and add them to the main schema
66 for (size_t i
= 0; i
< json_refs
.size(); ++i
) {
67 JSON_Tokenizer
* segment
= json_refs
.get_nth_elem(i
);
68 segment
->put_next_token(JSON_TOKEN_OBJECT_END
, NULL
);
69 insert_schema(json
, *segment
);
74 // end of the "anyOf" structure
75 json
.put_next_token(JSON_TOKEN_ARRAY_END
, NULL
);
78 // top-level object end
79 json
.put_next_token(JSON_TOKEN_OBJECT_END
, NULL
);
82 void Ttcn2Json::insert_schema(JSON_Tokenizer
& to
, JSON_Tokenizer
& from
)
84 json_token_t token
= JSON_TOKEN_NONE
;
85 char* value_str
= NULL
;
90 from
.get_next_token(&token
, &value_str
, &value_len
);
92 if (token
== JSON_TOKEN_ERROR
) {
93 FATAL_ERROR("Ttcn2Json::insert_schema");
96 if (value_str
!= NULL
) {
97 // put_next_token expects a null terminated string, save the next character
99 temp
= value_str
[value_len
];
100 value_str
[value_len
] = 0;
103 to
.put_next_token(token
, value_str
);
105 if (value_str
!= NULL
) {
106 // put the original character back to its place
107 value_str
[value_len
] = temp
;
109 } while (token
!= JSON_TOKEN_NONE
);
This page took 0.0337730000000001 seconds and 5 git commands to generate.