-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add json serialization customization options (#131)
* add json serialization customization options fixes #123. * v0.0.47 Co-authored-by: Tate <tate@transcriptic.com>
- Loading branch information
1 parent
51eb20a
commit 2b74bd7
Showing
18 changed files
with
5,094 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"json": { | ||
"emitFieldsWithDefaultValues": true, | ||
"useProtoFieldName": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# TwirpScript Config JSON Example | ||
|
||
## Getting Started | ||
|
||
### Running Locally | ||
|
||
1. `yarn install` | ||
2. `yarn build` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "config-json", | ||
"scripts": { | ||
"build": "yarn twirpscript && yarn tsc" | ||
}, | ||
"dependencies": { | ||
"twirpscript": "*" | ||
}, | ||
"devDependencies": { | ||
"typescript": "^4.3.5" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. | ||
// Source: src/A.proto | ||
|
||
import type { ByteSource } from "twirpscript"; | ||
import { BinaryReader, BinaryWriter } from "twirpscript"; | ||
|
||
//========================================// | ||
// Types // | ||
//========================================// | ||
|
||
export interface Foo { | ||
baz: number; | ||
fooBars: string[]; | ||
} | ||
|
||
//========================================// | ||
// Protobuf Encode / Decode // | ||
//========================================// | ||
|
||
export const Foo = { | ||
/** | ||
* Serializes a Foo to protobuf. | ||
*/ | ||
encode: function (foo: Partial<Foo>): Uint8Array { | ||
return Foo._writeMessage(foo, new BinaryWriter()).getResultBuffer(); | ||
}, | ||
|
||
/** | ||
* Deserializes a Foo from protobuf. | ||
*/ | ||
decode: function (bytes: ByteSource): Foo { | ||
return Foo._readMessage(Foo.initialize(), new BinaryReader(bytes)); | ||
}, | ||
|
||
/** | ||
* Serializes a Foo to JSON. | ||
*/ | ||
encodeJSON: function (foo: Partial<Foo>): string { | ||
return JSON.stringify(Foo._writeMessageJSON(foo)); | ||
}, | ||
|
||
/** | ||
* Deserializes a Foo from JSON. | ||
*/ | ||
decodeJSON: function (json: string): Foo { | ||
return Foo._readMessageJSON(Foo.initialize(), JSON.parse(json)); | ||
}, | ||
|
||
/** | ||
* Initializes a Foo with all fields set to their default value. | ||
*/ | ||
initialize: function (): Foo { | ||
return { | ||
baz: 0, | ||
fooBars: [], | ||
}; | ||
}, | ||
|
||
/** | ||
* @private | ||
*/ | ||
_writeMessage: function ( | ||
msg: Partial<Foo>, | ||
writer: BinaryWriter | ||
): BinaryWriter { | ||
if (msg.baz) { | ||
writer.writeInt32(1, msg.baz); | ||
} | ||
if (msg.fooBars?.length) { | ||
writer.writeRepeatedString(2, msg.fooBars); | ||
} | ||
return writer; | ||
}, | ||
|
||
/** | ||
* @private | ||
*/ | ||
_writeMessageJSON: function (msg: Partial<Foo>): Record<string, unknown> { | ||
const json: Record<string, unknown> = {}; | ||
json.baz = msg.baz; | ||
json.foo_bars = msg.fooBars; | ||
return json; | ||
}, | ||
|
||
/** | ||
* @private | ||
*/ | ||
_readMessage: function (msg: Foo, reader: BinaryReader): Foo { | ||
while (reader.nextField()) { | ||
const field = reader.getFieldNumber(); | ||
switch (field) { | ||
case 1: { | ||
msg.baz = reader.readInt32(); | ||
break; | ||
} | ||
case 2: { | ||
msg.fooBars.push(reader.readString()); | ||
break; | ||
} | ||
default: { | ||
reader.skipField(); | ||
break; | ||
} | ||
} | ||
} | ||
return msg; | ||
}, | ||
|
||
/** | ||
* @private | ||
*/ | ||
_readMessageJSON: function (msg: Foo, json: any): Foo { | ||
const baz = json.baz ?? json.baz; | ||
if (baz) { | ||
msg.baz = baz; | ||
} | ||
const fooBars = json.fooBars ?? json.foo_bars; | ||
if (fooBars) { | ||
msg.fooBars = fooBars; | ||
} | ||
return msg; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
syntax = "proto3"; | ||
|
||
message Foo { | ||
int32 baz = 1; | ||
repeated string foo_bars = 2; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"moduleResolution": "Node", | ||
"noEmitOnError": false, | ||
"noUnusedLocals": true, | ||
"outDir": "dist", | ||
"rootDir": ".", | ||
"strict": true, | ||
"target": "ES2019" | ||
}, | ||
"include": ["src", "out"] | ||
} |
Oops, something went wrong.