native-json-macro 1.0.8

Native JSON for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
use std::collections::HashMap;
use syn::{
    parse::{Parse, ParseStream},
    *,
};

//------------------- JSON Syntax ------------------------------
//
// object = { pair, ...}
// pair = key : value
// key = identifier
// array  = [value, ...]
// value =  object | array | expression
// expression = string | number | identifier

const ATTRIBUTES: &str =
    "#[derive(Serialize, Deserialize, Debug, Clone)]\n#[allow(non_camel_case_types)]\n";

#[derive(PartialEq, Clone, Copy)]
pub enum ValueType {
    NULL,
    OBJECT,
    ARRAY,
    EXPRESSION,
    DECLARE,
}

pub struct Array {
    pub items: Vec<Value>,
}

pub struct Pair {
    pub key: Ident,
    pub value: Value,
}

pub struct Object {
    pub name: String,
    pub pairs: Vec<Pair>,
}

pub struct Json {
    pub value: Value,
    pub id: i32,
    objects: Vec<Object>,
    arrays: Vec<Array>,
    expressions: Vec<String>,
}

pub struct Value {
    pub t: ValueType,
    pub i: usize,
}

impl Array {
    pub fn new() -> Self {
        Self { items: Vec::new() }
    }
}

impl Object {
    pub fn new() -> Self {
        Self {
            name: "".to_string(),
            pairs: Vec::new(),
        }
    }
}

struct ClassDict {
    map: HashMap<String, Value>,
}

impl ClassDict {
    fn new() -> Self {
        Self {
            map: HashMap::new(),
        }
    }

    fn set(&mut self, key: &String, value: &Value) {
        let v = Value {
            t: value.t,
            i: value.i,
        };
        self.map.insert(key.clone(), v);
    }
}

impl Json {
    pub fn new() -> Self {
        return Self {
            value: Value {
                t: ValueType::NULL,
                i: 0,
            },
            id: 0,
            objects: Vec::new(),
            arrays: Vec::new(),
            expressions: Vec::new(),
        };
    }

    pub fn get_object(&self, v: &Value) -> &Object {
        return &self.objects[v.i];
    }

    pub fn get_object_mut(&mut self, v: &Value) -> &mut Object {
        return &mut self.objects[v.i];
    }

    pub fn get_array(&self, v: &Value) -> &Array {
        return &self.arrays[v.i];
    }

    pub fn get_expression(&self, v: &Value) -> &String {
        return &self.expressions[v.i];
    }

    fn append_object(&mut self, v: Object) -> Value {
        self.objects.push(v);
        let i = self.objects.len() - 1;
        let t = ValueType::OBJECT;
        return Value { t, i };
    }

    fn append_array(&mut self, v: Array) -> Value {
        self.arrays.push(v);
        let i = self.arrays.len() - 1;
        let t = ValueType::ARRAY;
        return Value { t, i };
    }

    fn append_expression(&mut self, v: String) -> Value {
        self.expressions.push(v);
        let i = self.expressions.len() - 1;
        let t = ValueType::EXPRESSION;
        return Value { t, i };
    }

    // terminal
    fn parse_expression(&mut self, input: ParseStream) -> Result<Value> {
        let mut span = input.span();

        // expression with generic is allowed
        let output = input.step(|cursor| {
            let mut rest = *cursor;
            let mut nested = 0;
            let mut s = "".to_owned();
            while let Some((tt, next)) = rest.token_tree() {
                span = tt.span();
                let token = tt.to_string();
                if token == "<" {
                    nested += 1;
                } else if token == ">" {
                    nested -= 1;
                }
                s += &token;

                // peek
                let mut peek = "".to_owned();
                if let Some((lookhead, _)) = next.token_tree() {
                    peek = lookhead.to_string();
                }

                // terminal
                if nested == 0 && (peek == "," || next.eof()) {
                    return Ok((s, next));
                }

                rest = next;
            }
            return Err(cursor.error("expression was not terminated!"));
        });

        if output.is_err() {
            return Err(Error::new(span, "expected expression"));
        }

        let s = output.unwrap_or("".to_owned());
        let value = self.append_expression(s);

        return Ok(value);
    }

    fn parse_pair(&mut self, input: ParseStream) -> Result<Pair> {
        // key
        let key: Ident = input.parse()?;
        // :
        input.parse::<Token![:]>()?;
        // value
        let value = self.parse_value(&input)?;

        return Ok(Pair { key, value });
    }

    fn parse_declare(&mut self, input: ParseStream) -> Result<Value> {
        let name: Ident = input.parse()?;
        let mut value = self.parse_object(input)?;

        // using declared name
        let object = self.get_object_mut(&value);
        object.name = name.to_string();
        value.t = ValueType::DECLARE;

        return Ok(value);
    }

    // object := { key: value, ...}
    fn parse_object(&mut self, input: ParseStream) -> Result<Value> {
        let inner;
        let mut content;

        content = input;
        if input.peek(syn::token::Brace) {
            braced!(inner in input);
            content = &inner;
        }

        let mut object = Object::new();
        object.name = format!("Object{}", self.id);
        self.id += 1;

        loop {
            let pair = self.parse_pair(content)?;
            object.pairs.push(pair);
            if !content.peek(Token![,]) {
                break;
            }
            content.parse::<Token![,]>()?;
            if content.is_empty() {
                break;
            }
        }

        let v = self.append_object(object);
        return Ok(v);
    }

    // array := [value, ...]
    fn parse_array(&mut self, input: ParseStream) -> Result<Value> {
        let mut array = Array::new();

        let inner;
        let mut content = input;

        if input.peek(syn::token::Bracket) {
            bracketed!(inner in input);
            content = &inner;
        }

        loop {
            let value = self.parse_value(content)?;
            array.items.push(value);
            if !content.peek(Token![,]) {
                break;
            }
            content.parse::<Token![,]>()?;
            if content.is_empty() {
                break;
            }
        }

        let value = self.append_array(array);
        return Ok(value);
    }

    // value := object | array | expression
    fn parse_value(&mut self, input: ParseStream) -> Result<Value> {
        if input.peek(syn::token::Brace) {
            return self.parse_object(input);
        } else if input.peek(syn::token::Bracket) {
            return self.parse_array(input);
        }
        return self.parse_expression(input);
    }

    pub fn get_generics(&self) -> String {
        let mut defines = Vec::new();
        defines.push("".to_owned());

        for obj in &self.objects {
            let mut i = 0;
            let mut types = Vec::new();
            let mut fields = Vec::new();
            for pair in &obj.pairs {
                let t = format!("T{}", {
                    i += 1;
                    i
                });
                let key = pair.key.to_string();
                // rename
                let mut rename = "".to_string();
                if key.len() > 1 && key.ends_with("_") {
                    let name = &key[0..key.len() - 1];
                    rename = format!("#[serde(rename = \"{name}\")]\n");
                }
                let f = format!("{rename} {}:{}", key, t);
                types.push(t);
                fields.push(f);
            }
            let define = format!(
                "struct {}<{}> {{ {} }}",
                obj.name,
                types.join(","),
                fields.join(",")
            );
            defines.push(define);
        }

        return defines.join(ATTRIBUTES);
    }

    pub fn get_code(&self) -> String {
        let name = "".to_owned();
        let code = self.gen_code(&self.value, &name);
        return code;
    }

    pub fn get_block(&self) -> String {
        if self.value.t == ValueType::DECLARE {
            let path = "".to_owned();
            let (name, declare) = self.gen_declare(path, &self.value);
            let mut code = declare;
            // objects which require initializers
            let mut dict = ClassDict::new();
            dict = self.get_dict(dict, &name, &self.value);
            for (key, value) in &dict.map {
                let init = self.gen_initializer(key, value);
                let implement = format!(
                    "impl {} {{\n    pub fn new() -> Self {{\n        {}\n    }}\n}}\n",
                    key, init
                );
                code += &implement;
            }

            return code;
        } else {
            let prototypes = self.get_generics();
            let code = self.get_code();
            let block = format!("{{ {}\n{} }}", prototypes, code);
            return block;
        }
    }

    fn gen_code(&self, value: &Value, object_type: &String) -> String {
        let mut code;
        let none = "".to_owned();
        match value.t {
            ValueType::OBJECT => {
                let obj = self.get_object(value);
                let mut fields = Vec::new();
                for pair in &obj.pairs {
                    let v = self.gen_code(&pair.value, object_type);
                    let f = format!("{}:{}", pair.key.to_string(), v);
                    fields.push(f);
                }
                let name = if object_type.is_empty() {
                    &obj.name
                } else {
                    object_type
                };
                code = format!("{} {{ {} }}", name, fields.join(","));
            }
            ValueType::ARRAY => {
                let array = self.get_array(value);
                let mut item_type = &none;
                // use the first item type
                if array.items.len() > 0 && matches!(array.items[0].t, ValueType::OBJECT) {
                    let obj = self.get_object(&array.items[0]);
                    item_type = &obj.name;
                }
                let items: Vec<_> = array
                    .items
                    .iter()
                    .map(|x| {
                        let c = self.gen_code(x, &item_type);
                        c
                    })
                    .collect();
                code = format!("[{}]", items.join(","));
            }
            ValueType::EXPRESSION => {
                let expr = self.get_expression(value);
                code = expr.clone();
                if code.eq("null") || code.eq("None") {
                    code = "Option::<String>::None".to_owned();
                }
            }
            ValueType::DECLARE => {
                code = "TODO: declare".to_owned();
            }
            ValueType::NULL => {
                code = "Option::<String>::None".to_owned();
            }
        }
        return code;
    }

    fn get_instance(&self, class: &String) -> String {
        const PRIMITIVES: [&str; 15] = [
            "u8", "u16", "u32", "u64", "u128", "i8", "i16", "i32", "i64", "i128", "f32", "f64",
            "char", "isize", "usize",
        ];

        // our optional suffix
        let mut t = class.clone();
        if class.ends_with("?") {
            t = (&t[0..t.len() - 1]).into();
        }

        if t == "bool" {
            return "false".to_owned();
        }

        for c in PRIMITIVES {
            if c == &t {
                return format!("0 as {}", t);
            }
        }

        if class.find("Option<").is_some() {
            return "None".to_owned();
        }

        let mut c = class.as_str();

        // generic
        if let Some(i) = class.find("<") {
            c = &class[0..i];
        } else if class == "str" || class == "&str" {
            c = "std::string::String";
        } else if class.ends_with("?") {
            // our optional suffix
            c = &t;
        }

        // type must have new() initializer
        return format!("{}::new()", c);
    }

    // dict of (path, object)
    fn get_dict(&self, mut dict: ClassDict, path: &String, value: &Value) -> ClassDict {
        match value.t {
            ValueType::DECLARE | ValueType::OBJECT => {
                // initializer for object
                dict.set(path, value);
                let object = self.get_object(value);
                for pair in &object.pairs {
                    let child = path.clone() + "_" + &pair.key.to_string();
                    dict = self.get_dict(dict, &child, &pair.value);
                }
            }
            ValueType::ARRAY => {
                // initializer for array item
                let array = self.get_array(value);
                let child = path.clone() + "_item";
                dict = self.get_dict(dict, &child, &array.items[0]);
            }
            ValueType::EXPRESSION => {}
            ValueType::NULL => {}
        }

        return dict;
    }

    fn gen_initializer(&self, path: &String, value: &Value) -> String {
        let mut code = "".to_owned();

        match value.t {
            ValueType::DECLARE | ValueType::OBJECT => {
                let object = self.get_object(value);
                let mut fields = Vec::new();
                for pair in &object.pairs {
                    let child = path.clone() + "_" + &pair.key.to_string();
                    let c = self.gen_initializer(&child, &pair.value);
                    let key = pair.key.to_string();
                    let f = format!("{}: {}", key, c);
                    fields.push(f);
                }
                code += format!("{} {{ {} }}", path, fields.join(",")).as_str();
            }
            ValueType::ARRAY => {
                code = "std::vec::Vec::new()".to_owned();
            }
            ValueType::EXPRESSION => {
                let expr = self.get_expression(value);
                code = self.get_instance(&expr);
            }
            ValueType::NULL => {}
        }

        return code;
    }

    fn gen_declare(&self, mut path: String, value: &Value) -> (String, String) {
        // class of current node
        let mut class = "".to_owned();
        let mut code = "".to_owned();
        match value.t {
            ValueType::DECLARE | ValueType::OBJECT => {
                let object = self.get_object(value);
                if path.is_empty() {
                    path = object.name.clone();
                }
                class = path;
                let mut fields = Vec::new();
                for pair in &object.pairs {
                    let child = class.clone() + "_" + &pair.key.to_string();
                    let (mut n, c) = self.gen_declare(child, &pair.value);
                    code += &c;
                    let key = pair.key.to_string();
                    // optional
                    let mut optional = "";
                    if n.ends_with("?") {
                        n = (&n[0..n.len() - 1]).into();
                        optional = "skip_serializing_if = \"is_default\"";
                    }
                    // rename
                    let mut rename = "".to_string();
                    if key.len() > 1 && key.ends_with("_") {
                        let name = &key[0..key.len() - 1];
                        rename = format!("rename = \"{name}\"");
                    }
                    // attributes
                    let mut attributes = "".to_string();
                    if optional.len() > 0 || rename.len() > 0 {
                        attributes = "#[serde(default".into();
                        if optional.len() > 0 {
                            attributes += ",";
                            attributes += optional;
                        }
                        if rename.len() > 0 {
                            attributes += ",";
                            attributes += &rename;
                        }
                        attributes += ")]\n";
                    }
                    // collapse to "key: type"
                    let f = format!("{attributes}pub {}:{}", key, n);
                    fields.push(f);
                }
                let c = format!("pub struct {} {{\n{}\n}}\n", class, fields.join(",\n"));
                code += ATTRIBUTES;
                code += &c;
            }
            ValueType::ARRAY => {
                // array: [type]
                let array = self.get_array(value);
                let child = path + "_item";
                let (n, c) = self.gen_declare(child, &array.items[0]);
                code += &c;
                class = format!("std::vec::Vec<{}>", n);
            }
            ValueType::EXPRESSION => {
                // expression is type
                let v = self.get_expression(value);
                class = v.clone();
                if class == "str" || class == "&str" {
                    class = "String".to_owned();
                }
            }
            ValueType::NULL => {}
        }

        return (class, code);
    }
}

impl Parse for Json {
    fn parse(input: ParseStream) -> Result<Self> {
        let mut json = Json::new();

        if input.peek2(syn::token::Brace) {
            // declare := identifier { ... }
            json.value = json.parse_declare(input)?;
        } else if input.peek2(syn::token::Colon) {
            // value := object | array
            json.value = json.parse_object(input)?;
        } else {
            // array := [value, ...]
            json.value = json.parse_array(input)?;
        }

        return Ok(json);
    }
}