Skip to content

Commit

Permalink
Merge pull request weichx#80 from TheBinaryFox/mw/v2
Browse files Browse the repository at this point in the history
Added new way to create object instances when deserializing.
  • Loading branch information
weichx authored Feb 7, 2018
2 parents 8eb3df2 + bbb4f15 commit 9ee8e82
Show file tree
Hide file tree
Showing 8 changed files with 375 additions and 220 deletions.
37 changes: 31 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ Once you have annotated your class types, you can use the `Serialize*` and `Dese
// when deserializing our planet log we need to convert the timezone
// of the timeVisited value from galactic to local time
// (This could also be done via @deserializeUsing(Time.toLocalTime))
static onDeserialized(instance : PlanetLog, json : JsonObject, createInstances : boolean) {
static onDeserialized(instance : PlanetLog, json : JsonObject, instantiationMethod : boolean) {
instance.timeVisited = Time.toLocalTime(instance.timeVisited);
}

Expand Down Expand Up @@ -168,7 +168,7 @@ If you want the same behavior for a property when serializing and deserializing,
##### Types
```typescript
type SerializationFn = <T>(target : T) => JsonType;
type DeserializationFn = <T>(data : JsonType, target? : T, createInstances? : boolean) => T
type DeserializationFn = <T>(data : JsonType, target? : T, instantiationMethod? : InstantiationMethod) => T
type SerializeAndDeserializeFns = {
Serialize: SerializationFn,
Deserialize: DeserializationFn
Expand Down Expand Up @@ -230,7 +230,32 @@ It is also possible to re-use existing objects when deserializing with `Deserial

#### Deserializing Into Plain Objects

The `createInstances` parameter can be used to toggle between creating actual instances of the input type, or when false the `deserializeXXX` functions will return a plain object instead. This can be useful for systems like Redux which expect / require plain objects and not class instances.
The `instantiationMethod` parameter can be used to change the way in which instances of the input type are created. With `InstantiationMethod.New`, the constructor will be invoked when a new instance needs to be created. With `InstantiationMethod.ObjectCreate`, the object will be created without invoking its constructor, which is useful for systems where constructed objects immediately freeze themselves. With `InstantiationMethod.None`, the `deserializeXXX` functions will return a plain object instead, which can be useful for systems like Redux that expect / require plain objects and not class instances.

```typescript
import {Deserialize, Instances} from 'cerialize';
class Immutable {
public value : string;
constructor(value : string) {
this.value = value;
Object.freeze(this);
}
public getValue() : string {
return value;
}
}
Deserialize({value: 'example'}, Immutable, InstantiationMethod.New); // Error because of Object.freeze
Deserialize({value: 'example'}, Immutable, InstantiationMethod.ObjectCreate); // Immutable {value 'example'}
Deserialize({value: 'example'}, Immutable, InstantiationMethod.None); // Object {value: 'example'}
```

The default InstantiationMethod can be changed with `SetDefaultInstantiationMethod(instantiationMethod : InstantiationMethod)`

##### Functions
- `Deserialize<T>(json : JsonObject, ClassConstructor<T>, target? : T) : T`
Expand Down Expand Up @@ -353,15 +378,15 @@ A callback can be provided for when a class is serialized. To define the callbac
```

## onDeserialized Callback
A callback can be provided for when a class is deserialized. To define the callback, add a static method `onDeserialized<T>(instance : T, json : JsonObject, createInstances = true)` to the class that needs custom post processing. You can either return a new value from this function, or modify the `json` parameter. The `createInstances` parameter signifies whether the initial call to deserialize this object should create instances of the types (when true) or just plain objects (when false)
A callback can be provided for when a class is deserialized. To define the callback, add a static method `onDeserialized<T>(instance : T, json : JsonObject, instantiationMethod = InstantationMethod.New)` to the class that needs custom post processing. You can either return a new value from this function, or modify the `json` parameter. The `instantiationMethod` parameter signifies whether the initial call to deserialize this object should create instances of the types (when true) or just plain objects (when false)

```typescript
class CrewMember {

@autoserializeAs(String) firstName;
@autoserializeAs(String) lastName;

static onDeserialized(instance : CrewMember, json : JsonObject, createInstances : boolean) {
static onDeserialized(instance : CrewMember, json : JsonObject, instantiationMethod : InstantiationMethod) {
instance.firstName = json.firstName.toLowerCase();
instance.lastName = json.lastName.toLowerCase();
}
Expand Down Expand Up @@ -413,4 +438,4 @@ When using `SetDeserializeKeyTransform(fn : (str : string) => string)` you need
expect(instance).toEqual({
value: "strvalue"
});
```
```
Loading

0 comments on commit 9ee8e82

Please sign in to comment.