Giter Club home page Giter Club logo

Comments (11)

igdeman avatar igdeman commented on August 17, 2024 7

Hi guys, sorry on delay, a bit busy this days :(
Here is working example using TypeScript:

class Assignable
{
    [index:string]:any;
    constructor(properties:{[index:string]:any}) 
    {
        Object.keys(properties).map((key:string) => {
            this[key] = properties[key];
        });
    }
}
class ModelExample extends Assignable
{
    byteArray?:Uint8Array;
    constructor(properties:{[index:string]:any})
    {
        super(properties);
    }
}
const scheme = new Map([
    [ModelExample, { 
        kind: 'struct', 
        fields: [
            ['byteArray', [10]],
        ] 
    }],
]);

let byteArray = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
let exampleTest:ModelExample = new ModelExample({byteArray})

let exampleSerialized = borsh.serialize(scheme, exampleTest);
let exampleDeserialized = borsh.deserialize(scheme, ModelExample, Buffer.from(exampleSerialized));

console.log(exampleDeserialized);

I hope this helps...

from borsh-js.

igdeman avatar igdeman commented on August 17, 2024 2

Have similar issue. In my case serialization of Uint8Array works fine but unfortunately deserialization doesn't. I'm sure that buffer i'm pasing for deserialization is filed correctly but result of desserialization is just an empty model.

My model has fields declaration like this:
first_name: Uint8Array = new Uint8Array(20); last_name: Uint8Array = new Uint8Array(20); username: Uint8Array = new Uint8Array(20); email: Uint8Array = new Uint8Array(128);
Sheme:
new Map([ [UserAccount, { kind: 'struct', fields: [ ['first_name', [20]], ['last_name', [20]], ['username', [20]], ['email', [128]], ] }], ]);

Actually deserialization also works fine! I have figured out that model class has to have constructor that accepts object with deserialized params.

from borsh-js.

jsoneaday avatar jsoneaday commented on August 17, 2024

I have a similar issue where I'm trying to serialize/deserialize an array of some type. Does anyone know how to do that?

from borsh-js.

igdeman avatar igdeman commented on August 17, 2024

Have similar issue. In my case serialization of Uint8Array works fine but unfortunately deserialization doesn't. I'm sure that buffer i'm pasing for deserialization is filed correctly but result of desserialization is just an empty model.

My model has fields declaration like this:
first_name: Uint8Array = new Uint8Array(20); last_name: Uint8Array = new Uint8Array(20); username: Uint8Array = new Uint8Array(20); email: Uint8Array = new Uint8Array(128);
Sheme:
new Map([ [UserAccount, { kind: 'struct', fields: [ ['first_name', [20]], ['last_name', [20]], ['username', [20]], ['email', [128]], ] }], ]);

from borsh-js.

FrankC01 avatar FrankC01 commented on August 17, 2024

@igdeman Can you post a simple working model that succeeds? Thanks...

from borsh-js.

jsoneaday avatar jsoneaday commented on August 17, 2024

But what if you have an array of UserAccount? How do you handle that?

from borsh-js.

jsoneaday avatar jsoneaday commented on August 17, 2024

@igdeman I'm not sure what you mean by object with deserialized params. For example here is my own model below. How would I refactor it in order to allow deserialization of an array of this model type?

class ChatMessage { archive_id: string = DUMMY_TX_ID; created_on: string = DUMMY_CREATED_ON; // max milliseconds in date constructor( fields: { archive_id: string; created_on: string } | undefined = undefined ) { if (fields) { this.archive_id = fields.archive_id; this.created_on = fields.created_on; } } }

from borsh-js.

max-block avatar max-block commented on August 17, 2024

I still have the error TypeError: writer[capitalizeFirstLetter(...)] is not a function

The last example doesn't work for me. It fails with the error.

from borsh-js.

AuroraLantean avatar AuroraLantean commented on August 17, 2024

igdeman thank you so much. Now I can convert bignumber array into string array, then use str.charCodeAt(char_index) to convert each string char into UTF-16 code, which is between 0 and 255.
Also I convert boolean array into number array, then new Uint8Array(boolnum_array);

const scheme = new Map([
  [Struct1, { 
      kind: 'struct', 
      fields: [
          ['byteArray1', [dataLength1]],
          ['byteArray2', [dataLength2]],
          ['byteArray3', [dataLength3]],
      ] 
  }],
]);
const byteArray1 = new Uint8Array(utf16Array1);
const byteArray2 = new Uint8Array(utf16Array2);
const byteArray3 = new Uint8Array(utf16Array3);

let obj_input:Struct1 = new Struct1({
  byteArray1: byteArray1,
  byteArray2: byteArray2,
  byteArray3: byteArray3,
});
let serialized = borsh.serialize(scheme, obj_input);

from borsh-js.

marcus-pousette avatar marcus-pousette commented on August 17, 2024

I see you found a solution to this, but I want to present a solution of my own, that perhaps makes the code a bit cleaner.

I am maintaining a fork of this library that lets you do this

class SomeClass {
  @field({ type: fixedArray('u8', 3) }) // Fixed array of length 3
  public fixedLengthArray: number[];
}

... 

let ser = serialize(new SomeClass(...))
let der = deserialize(Buffer.from(ser), SomeClass)

You can replace the type "number[]" with UInt8Array and it would also work for serialization but would deserialize into "number[]" natively.

from borsh-js.

gagdiez avatar gagdiez commented on August 17, 2024

should be fixed now, please open again if not

from borsh-js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.