Comments (11)
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.
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.
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.
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.
@igdeman Can you post a simple working model that succeeds? Thanks...
from borsh-js.
But what if you have an array of UserAccount? How do you handle that?
from borsh-js.
@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.
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.
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.
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.
should be fixed now, please open again if not
from borsh-js.
Related Issues (20)
- deserialize method not mapping properties properly HOT 1
- How to serialize or create schema for vec of structs HOT 1
- CI is not working HOT 1
- yarn build and yarn test fails
- TypeError: reader[capitalizeFirstLetter(...)] is not a function HOT 2
- Deserializing Option type should return null in the None case HOT 1
- Schemaless serialization? HOT 1
- Serialize primitives (not structs)? HOT 2
- Security Policy violation Branch Protection HOT 2
- Security Policy violation SECURITY.md HOT 2
- Deserialize requires type assertion HOT 2
- Non-ASCII characters not being properly serialized HOT 2
- How to deserialize enum? HOT 1
- Enum schema failed with typescript check when (de)serialize HOT 2
- How to (de)serialize unnamed fields HOT 2
- Type is not strict when serialize `u64` or `u128` HOT 1
- Add `u256` HOT 1
- Did not check overflow when serialize integers
- Should we impl the schema util for borsh-js
- Is it possible to have a property with 2 possible values for the length in a borsh-js(v.1.0.0) schema? HOT 10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from borsh-js.