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

automatic (de)serialization to Variant dicts #104

Open
mikelpr opened this issue Feb 8, 2022 · 0 comments
Open

automatic (de)serialization to Variant dicts #104

mikelpr opened this issue Feb 8, 2022 · 0 comments

Comments

@mikelpr
Copy link

mikelpr commented Feb 8, 2022

I wrote the following for myself as I wanted to use D-Bus natives and not send JSON all over the place and I didn't see the library provide anything for using them Variants

import {Variant} from "dbus-next";

export function serialize(obj: any): Variant<any> | undefined {
  if (obj === undefined) return undefined;
  if (typeof obj == 'function') throw new Error("can't serialize function");
  const signature = (({
    'object': () => obj === null? 'o':(Array.isArray(obj)? 'av':'a{sv}'),
    'string': () => 's',
    'number': () => 'd',
   'boolean': () => 'b',
    'symbol': () => 's',
    'bigint': () => 'x'
  } as any)[typeof obj]) ();

  let _ = obj;
  if (typeof obj == 'object') {
    if (obj === null) _ = '/dev/null';
    else {
      _ = Array.isArray(obj)? []:{};
      for (const k in obj) {
        if (obj[k] !== undefined) _[k] = serialize(obj[k]);
      }
    }
  }

  return new Variant(signature, _);
};

type Key = string | number | symbol;
export function deserialize(v: Variant): any {
  const sig = v.signature.slice(0, 2);
  if (sig == 'av') return v.value.map(deserialize);
  else if (sig == 'a{') {
    if (sig[4] !== '}') throw new Error("invalid variant signature");
    //const kt = sig[2]; // unused
    const vt = sig[3];
    return Object.entries(v.value).reduce(
      (acc: {[x: Key]: any}, [key, value]: [Key, any]) =>
        ({...acc, [key]: vt === 'v'? deserialize(value):value}), {}
    );
  }
  else if (sig == 'o') return v.value === '/dev/null'? null:v.value;
  else return v.value;
}

but like, would be cool if the library did this automatically. convert param objects and returns into a{sv} and variant type arrays into av. so for example you could

someObj = {};
@property({signature: 'v'}) // or just @property({})
get Configuration() {return someObj}
set Configuration(newObj) {someObj = newObj}

instead of having to

someObj = {};
@property({signature: 'v'}) // or just @property({})
get Configuration() {return serialize(someObj)}
set Configuration(newObj) {someObj = deserialize(newObj)}
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

1 participant