Skip to content

Commit

Permalink
Merge pull request #273 from betwixt-labs/to-JSON
Browse files Browse the repository at this point in the history
fix!: refactor generated JSON methods from toJson to toJSON
  • Loading branch information
andrewmd5 authored Jun 27, 2023
2 parents eccb66d + f816647 commit 7a74e9e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
4 changes: 2 additions & 2 deletions .env
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION="2.8.4"
VERSION="2.8.5"
MAJOR=2
MINOR=8
PATCH=4
PATCH=5
18 changes: 9 additions & 9 deletions Core/Generators/TypeScript/TypeScriptGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,14 +315,14 @@ public string CompileJsonMethods(Definition definition)
{
var builder = new IndentedStringBuilder(0);
builder.AppendLine(FormatDocumentation("Serializes the current instance into a JSON-Over-Bebop string", string.Empty, 0));
builder.CodeBlock($"public toJson(): string", indentStep, () =>
builder.CodeBlock($"public toJSON(): string", indentStep, () =>
{
builder.AppendLine($"return {definition.ClassName()}.encodeToJson(this);");
builder.AppendLine($"return {definition.ClassName()}.encodeToJSON(this);");
});
builder.AppendLine();

builder.AppendLine(FormatDocumentation("Serializes the specified object into a JSON-Over-Bebop string", string.Empty, 0));
builder.CodeBlock($"public static encodeToJson(record: I{definition.ClassName()}): string", indentStep, () =>
builder.CodeBlock($"public static encodeToJSON(record: I{definition.ClassName()}): string", indentStep, () =>
{
if (definition is UnionDefinition)
{
Expand Down Expand Up @@ -365,11 +365,11 @@ public string CompileJsonMethods(Definition definition)
});
builder.AppendLine();
builder.AppendLine(FormatDocumentation($"Creates a new {{@link {definition.ClassName()}}} instance from a JSON-Over-Bebop string. Type checking is performed.", string.Empty, 0));
builder.CodeBlock($"public static fromJson(json: string): {returnType}", indentStep, () =>
builder.CodeBlock($"public static fromJSON(json: string): {returnType}", indentStep, () =>
{
builder.CodeBlock("if (typeof json !== 'string' || json.trim().length === 0)", indentStep, () =>
{
builder.AppendLine($"throw new BebopRuntimeError(`{definition.ClassName()}.fromJson: expected string`);");
builder.AppendLine($"throw new BebopRuntimeError(`{definition.ClassName()}.fromJSON: expected string`);");
});
builder.AppendLine("const parsed = JSON.parse(json, BebopJson.reviver);");
builder.AppendLine($"{definition.ClassName()}.validateCompatibility(parsed);");
Expand Down Expand Up @@ -976,8 +976,8 @@ public override string Compile(Version? languageVersion, TempoServices services
builder.AppendLine($"invoke: service.{methodName},");
builder.AppendLine($"serialize: {method.Definition.ReturnDefintion}.encode,");
builder.AppendLine($"deserialize: {method.Definition.RequestDefinition}.decode,");
builder.AppendLine($"toJson: {method.Definition.ReturnDefintion}.encodeToJson,");
builder.AppendLine($"fromJson: {method.Definition.RequestDefinition}.fromJson,");
builder.AppendLine($"toJSON: {method.Definition.ReturnDefintion}.encodeToJSON,");
builder.AppendLine($"fromJSON: {method.Definition.RequestDefinition}.fromJSON,");
builder.AppendLine($"type: MethodType.{RpcSchema.GetMethodTypeName(methodType)},");
}, close: $"}} as BebopMethod<I{method.Definition.RequestDefinition}, I{method.Definition.ReturnDefintion}>);");
}
Expand Down Expand Up @@ -1049,8 +1049,8 @@ public override string Compile(Version? languageVersion, TempoServices services
builder.AppendLine($"id: {method.Id},");
builder.AppendLine($"serialize: {method.Definition.RequestDefinition}.encode,");
builder.AppendLine($"deserialize: {method.Definition.ReturnDefintion}.decode,");
builder.AppendLine($"toJson: {method.Definition.RequestDefinition}.encodeToJson,");
builder.AppendLine($"fromJson: {method.Definition.ReturnDefintion}.fromJson,");
builder.AppendLine($"toJSON: {method.Definition.RequestDefinition}.encodeToJSON,");
builder.AppendLine($"fromJSON: {method.Definition.ReturnDefintion}.fromJSON,");
builder.AppendLine($"type: MethodType.{RpcSchema.GetMethodTypeName(methodType)},");
});

Expand Down
6 changes: 3 additions & 3 deletions Laboratory/TypeScript/benchmark/jsonoverbebop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const song = new G.Song({
],
});

const encodedJsonLib = song.toJson();
const encodedJsonLib = song.toJSON();
const encodedBinaryLib = song.encode();
const standardJson = JSON.stringify(song);
console.log(standardJson)
Expand All @@ -23,7 +23,7 @@ suite.add(`encode binary bebop`, () => {
G.Song.encode(song);
});
suite.add(`encode json-over-bebop`, function () {
G.Song.encodeToJson(song);
G.Song.encodeToJSON(song);
});

suite.add(`encode standard json`, function () {
Expand All @@ -35,7 +35,7 @@ suite.add(`decode binary bebop`, () => {
});

suite.add(`decode json-over-bebop`, function () {
G.Song.fromJson(encodedJsonLib);
G.Song.fromJSON(encodedJsonLib);
});

suite.add(`decode standard json`, function () {
Expand Down
14 changes: 7 additions & 7 deletions Laboratory/TypeScript/test/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ it("Roundtrips a flat object to JSON-Over-Bebop", () => {
a_date: new Date(Date.UTC(1996, 1, 7)),
};

const json = G.BasicTypes.encodeToJson(obj);
const json = G.BasicTypes.encodeToJSON(obj);
const rawParse = JSON.parse(json);
expect(rawParse).toMatchObject({
a_bool: true,
Expand All @@ -35,7 +35,7 @@ it("Roundtrips a flat object to JSON-Over-Bebop", () => {
a_guid: { "#btype": 5, value: "01234567-0123-0123-0123-0123456789ab" },
a_date: { "#btype": 2, value: "629592480000000000" },
});
expect(() => G.BasicTypes.fromJson(json)).not.toThrow();
expect(() => G.BasicTypes.fromJSON(json)).not.toThrow();
});

it("Fails to roundtrip a flat object to JSON-Over-Bebop", () => {
Expand All @@ -58,7 +58,7 @@ it("Fails to roundtrip a flat object to JSON-Over-Bebop", () => {
obj,
(key, value) => (typeof value === "bigint" ? value.toString() : value) // return everything else unchanged
);
expect(() => G.BasicTypes.fromJson(json)).toThrow(BebopRuntimeError);
expect(() => G.BasicTypes.fromJSON(json)).toThrow(BebopRuntimeError);
});

it("Roundtrips a nested map object to JSON-Over-Bebop", () => {
Expand Down Expand Up @@ -94,8 +94,8 @@ it("Roundtrips a nested map object to JSON-Over-Bebop", () => {
],
]),
};
const json = G.SomeMaps.encodeToJson(obj);
expect(() => G.SomeMaps.fromJson(json)).not.toThrow();
const json = G.SomeMaps.encodeToJSON(obj);
expect(() => G.SomeMaps.fromJSON(json)).not.toThrow();
});

it("Roundtrips jazz to JSON-Over-Bebop", () => {
Expand All @@ -117,6 +117,6 @@ it("Roundtrips jazz to JSON-Over-Bebop", () => {
]),
};

const json = G.Library.encodeToJson(lib);
expect(() => G.Library.fromJson(json)).not.toThrow();
const json = G.Library.encodeToJSON(lib);
expect(() => G.Library.fromJSON(json)).not.toThrow();
});

0 comments on commit 7a74e9e

Please sign in to comment.