gyan deep
gyan deep

Reputation: 1900

dynamic id and special characters for serialization with flat buffers

I have Json data like below

{
  "!type": "alarm",
  "$": {
    "12279": {
      "!type": "alarm",
      "title": "Default",
      "$": {
        "5955": {
          "!type": "alarm",
          "name": "Wake",
          "day": "SUN",
          "startTime": "06:00"
        },
        "29323": {
          "!type": "alarm",
          "name": "Away",
          "day": "SUN",
          "startTime": "08:00"
        },
        "2238": {
          "!type": "alarm",
          "name": "Home",
          "day": "SUN",
          "startTime": "18:00"
        }
      }
    }
  }
}

My fbs looks like this

namespace space.alarm;

table Atom{
    !type:string;
    name:string;
    startDay:string;
    startTime:string; }

table AtomShell{
    key:string (required, key);
    value: Atom; }

table Alarm{
    !type:string;
    title:string;
    $:[AtomShell]; }


table AlarmShell{
    key:string (required, key);
    value:Alarm;  }


table Weeklyalarm{
    !type:string;
    $:[AlarmShell]; } root_type Weeklyalarm;

Im trying to implement google flat buffers but I'm getting errors like

  1. alarm.fbs:4:0: error: illegal character: !
  2. alarm.fbs:23:0: error: illegal character: $ (i have removed ! from !type and changed $ to dollar to test the working of flat buffers but i can't change the dynamic ids )
  3. Sample.json:25:0: error: unknown field: 12279

Now my question,

  1. Is it possible to use dynamic ids in flat buffers, if possible how shall i proceed?
  2. Can is use special characters in ids, if possible how to do it?

Thanks in advance.

Upvotes: 0

Views: 308

Answers (1)

Aardappel
Aardappel

Reputation: 6074

You can't have characters like ! and $ in field names. Just use type instead of !type, etc.

Not sure what you mean by dynamic ids. All field names (keys) have to be declared in the schema, so they can't be dynamic. You can still achieve similar results though, if you make your JSON look something like this:

{
  "type": "alarm",
  "data": [
    {
      id: "12279",
      "type": "alarm",
      "title": "Default",
      "data": [
        {
          "id": "5955",
          "type": "alarm",
          "name": "Wake",
          "day": "SUN",
          "startTime": "06:00"
        },
        {
          "id": "29323",
          "type": "alarm",
          "name": "Away",
          "day": "SUN",
          "startTime": "08:00"
        },
        {
          "id": "2238",
          "type": "alarm",
          "name": "Home",
          "day": "SUN",
          "startTime": "18:00"
        }
      ]
    }
  ]
}

And then make the corresponding schema.

Note that I made the "dynamic" list into a vector, and moved the id into the object itself.

Other tip: string values that are not dynamic (like "alarm") will take up way less space if you make them into an enum instead.

Upvotes: 0

Related Questions