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

Serialize unnamed records as immutable.Records #9

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Parsing unknown Records
  • Loading branch information
Piotr Wyrobek committed Oct 10, 2016

Verified

This commit was signed with the committer’s verified signature.
addaleax Anna Henningsen
commit 67af41f79206038e6d3de92de93f0b97fdbc4042
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -62,7 +62,27 @@ const result = deserialize(json, {

Record types can be named. This is utilized by the serializer/deserializer to revive `immutable.Record` objects. See the `SampleRecord` name passed into `immutable.Record()` as the second argument.

NOTE: When an unknown record type is encountered during deserialization, an error is thrown.
### Unknown Records

```javascript
const SampleRecord = immutable.Record(
{ 'a': 3, 'b': 4 },
'SampleRecord'
)

const data = {
'x': SampleRecord({ 'a': 5 }),
}

// Serialize
const json = serialize(data)
// json == '{"x":{"__record":"SampleRecord","data":{"a":5}}}'

// Deserialize
const result = deserialize(json, {
parseUnknownRecords: true
})
```

### General Immutable Structures

@@ -108,6 +128,7 @@ NOTE: When an unknown Immutable iterable type is encountered during deserializat
- `json`: A JSON representation of data.
- `options={}`: Deserialization options.
- `recordTypes={}`: `immutable.Record` factories.
- `parseUnknownRecords=true`: deserialize unknown `immutable.Record`

Return value:

6 changes: 5 additions & 1 deletion src/deserialize.js
Original file line number Diff line number Diff line change
@@ -27,8 +27,12 @@ function revive(key, value, options) {


function reviveRecord(key, recInfo, options) {
const RecordType = options.recordTypes[recInfo['__record']]
const RecordType = options.recordTypes && options.recordTypes[recInfo['__record']]
if (!RecordType) {
if (options.parseUnknownRecords) {
var TmpRecordType = new immutable.Record(recInfo['data']);
return TmpRecordType(revive(key, recInfo['data'], options))
}
throw new Error(`Unknown record type: ${recInfo['__record']}`)
}

89 changes: 88 additions & 1 deletion test/deserialize/record.js
Original file line number Diff line number Diff line change
@@ -25,7 +25,6 @@ it('should deserialize a record of a known type', (test) => {
})
})


it('should not deserialize a record of an unknown type', (test) => {
const data = {
'__record': 'SampleRecord',
@@ -43,6 +42,26 @@ it('should not deserialize a record of an unknown type', (test) => {
})


it('should deserialize a record of an unknown type', (test) => {
const data = {
'__record': 'SampleRecord',
'data': {
'a': 5,
'b': 6,
},
}

const UnknownRecord = immutable.Record({
'a': 1,
'b': 2,
})

helpers.testDeserialization(test, data, UnknownRecord(data['data']), {
recordTypes: {},
parseUnknownRecords: true
})
})

it('should deserialize nested records of known types', (test) => {
const RecordA = immutable.Record({
'a': 1,
@@ -77,3 +96,71 @@ it('should deserialize nested records of known types', (test) => {
},
})
})


it('should deserialize nested records of unknown and know types', (test) => {
const RecordA = immutable.Record({
'a': 1,
'b': 2,
})
const RecordB = immutable.Record({
'c': 3,
}, 'RecordB')

const data = {
'__record': 'RecordA',
'data': {
'a': 5,
'b': {
'__record': 'RecordB',
'data': {
'c': 6,
},
},
},
}

const expectedResult = RecordA({
'a': data['data']['a'],
'b': RecordB(data['data']['b']['data']),
})

helpers.testDeserialization(test, data, expectedResult, {
recordTypes: {
'RecordB': RecordB,
},
parseUnknownRecords: true
})
})

it('should deserialize nested records of unknown types', (test) => {
const RecordA = immutable.Record({
'a': 1,
'b': 2,
})
const RecordB = immutable.Record({
'c': 3,
})

const data = {
'__record': 'RecordA',
'data': {
'a': 5,
'b': {
'__record': 'RecordB',
'data': {
'c': 6,
},
},
},
}

const expectedResult = RecordA({
'a': data['data']['a'],
'b': RecordB(data['data']['b']['data']),
})

helpers.testDeserialization(test, data, expectedResult, {
parseUnknownRecords: true
})
})