Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java Support? #27

Open
jfuehner opened this issue Sep 24, 2021 · 1 comment
Open

Java Support? #27

jfuehner opened this issue Sep 24, 2021 · 1 comment

Comments

@jfuehner
Copy link

Was curious if it is possible to convert Java objects into a View and sent to browser for JavaScript consumption? Any examples?

@zandaqo
Copy link
Owner

zandaqo commented Sep 25, 2021

Sure. View objects of fixed size are just raw buffers where data is laid out sequentially without any padding, pointers etc. For example, the following schema:

{
  $id: "Pet",
  type: "object",
  properties: {
    name: { type: "string", maxLength: 6 },
    age: { type: "number", btype: "uint8" },
  },
};

results in a buffer where 6 bytes of UTF8 string are followed by one byte integer, 7 bytes total. This sort of buffer or byte array can be created in any language supporting byte manipulations. In C/C++ one can just use structs with #pragma pack(1) directive to disable alignment. I am not sure what would be the easiest way of doing the same in Java, but it does have a number of primitives working with bytes, including ByteBuffer class, so something along the following lines should work:

...
  // get utf8 bytes of our name string;
  byte[] name = "Arthur".getBytes("UTF-8");
  // encode age as one byte
  byte age = 10;
  // allocate 7 bytes of buffer
  ByteBuffer buffer = ByteBuffer.allocate(7);
  // write the name at the beginning 
  buffer.put(name);
  // write age as the last byte
  buffer.put(6, age);
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants