xref: /aosp_15_r20/external/flatbuffers/samples/sample_text.lobster (revision 890232f25432b36107d06881e0a25aaa6b473652)
1*890232f2SAndroid Build Coastguard Worker// Copyright 2018 Google Inc. All rights reserved.
2*890232f2SAndroid Build Coastguard Worker//
3*890232f2SAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License");
4*890232f2SAndroid Build Coastguard Worker// you may not use this file except in compliance with the License.
5*890232f2SAndroid Build Coastguard Worker// You may obtain a copy of the License at
6*890232f2SAndroid Build Coastguard Worker//
7*890232f2SAndroid Build Coastguard Worker//     http://www.apache.org/licenses/LICENSE-2.0
8*890232f2SAndroid Build Coastguard Worker//
9*890232f2SAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software
10*890232f2SAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS,
11*890232f2SAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*890232f2SAndroid Build Coastguard Worker// See the License for the specific language governing permissions and
13*890232f2SAndroid Build Coastguard Worker// limitations under the License.
14*890232f2SAndroid Build Coastguard Worker
15*890232f2SAndroid Build Coastguard Workerimport from "../lobster/"
16*890232f2SAndroid Build Coastguard Workerimport monster_generated
17*890232f2SAndroid Build Coastguard Worker
18*890232f2SAndroid Build Coastguard Worker// Example how to interop with JSON.
19*890232f2SAndroid Build Coastguard Worker
20*890232f2SAndroid Build Coastguard Worker// Test loading some JSON, converting it to a binary FlatBuffer and back again.
21*890232f2SAndroid Build Coastguard Worker
22*890232f2SAndroid Build Coastguard Worker// First read the schema and JSON data.
23*890232f2SAndroid Build Coastguard Workerlet schema = read_file("monster.fbs", true)
24*890232f2SAndroid Build Coastguard Workerlet json = read_file("monsterdata.json", true)
25*890232f2SAndroid Build Coastguard Workerassert schema and json
26*890232f2SAndroid Build Coastguard Worker
27*890232f2SAndroid Build Coastguard Worker// Parse JSON  to binary:
28*890232f2SAndroid Build Coastguard Workerlet fb, err1 = flatbuffers_json_to_binary(schema, json, [])
29*890232f2SAndroid Build Coastguard Workerassert not err1
30*890232f2SAndroid Build Coastguard Worker
31*890232f2SAndroid Build Coastguard Worker// Access one field in it, just to check:
32*890232f2SAndroid Build Coastguard Workerlet monster = MyGame_Sample_GetRootAsMonster(fb)
33*890232f2SAndroid Build Coastguard Workerassert monster.name == "Orc"
34*890232f2SAndroid Build Coastguard Worker
35*890232f2SAndroid Build Coastguard Worker// Convert binary back to JSON:
36*890232f2SAndroid Build Coastguard Workerlet json2, err2 = flatbuffers_binary_to_json(schema, fb, [])
37*890232f2SAndroid Build Coastguard Workerassert not err2
38*890232f2SAndroid Build Coastguard Worker
39*890232f2SAndroid Build Coastguard Worker// The generated JSON should be exactly equal to the original!
40*890232f2SAndroid Build Coastguard Workerassert json == json2
41*890232f2SAndroid Build Coastguard Worker
42*890232f2SAndroid Build Coastguard Worker// Print what we've been converting for good measure:
43*890232f2SAndroid Build Coastguard Workerprint json
44