-
-
-
- Use Newtonsoft Annotations
-
-
-
-
-
+
+
+
+
+
+ Use Pascal case
+
+
+ Use Record types
+
+
+ Generate Immutable classes
+
+
+ Use fields
+
+
+ Add JsonProperty attributes
+
+
+ Use NullValueHandling.Ignore
+
+
+ Add JsonPropertyName attributes
+
+
+ Use nullable types
+
+
+ Use Readonly lists
+
+
+
+
+
+
+
+
diff --git a/src/tools/json-to-csharp/json2csharp.d.ts b/src/tools/json-to-csharp/json2csharp.d.ts
deleted file mode 100644
index 990ecc1f..00000000
--- a/src/tools/json-to-csharp/json2csharp.d.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-declare module 'json2csharp'{
- export default function(json: string, useNewtonsoftAnnotations?: boolean): string;
-}
\ No newline at end of file
diff --git a/src/tools/json-to-csharp/json2csharp.test.ts b/src/tools/json-to-csharp/json2csharp.test.ts
new file mode 100644
index 00000000..52750f75
--- /dev/null
+++ b/src/tools/json-to-csharp/json2csharp.test.ts
@@ -0,0 +1,617 @@
+import { describe, expect, it } from 'vitest';
+import { getPrimitiveProp, isDate, json2classes, json2csharp } from './json2csharp';
+
+// Test isDate function
+describe('isDate', () => {
+ it('should return true for valid date strings', () => {
+ expect(isDate('2024-03-08')).toBe(true);
+ expect(isDate('2024-03-08T12:34:56Z')).toBe(true);
+ });
+
+ it('should return false for invalid date strings', () => {
+ expect(isDate('hello')).toBe(false);
+ expect(isDate('2024-13-08')).toBe(false); // Invalid month
+ expect(isDate('9999-99-99')).toBe(false); // Invalid format
+ });
+});
+
+// Test getPrimitiveProp function
+describe('getPrimitiveProp', () => {
+ it('should return \'string\' type for non-date strings', () => {
+ expect(getPrimitiveProp('hello', 'testKey')).toEqual({
+ type: 'string',
+ name: 'testKey',
+ canBeNullable: false,
+ });
+ });
+
+ it('should return \'DateTime\' type for date strings', () => {
+ expect(getPrimitiveProp('2024-03-08', 'dateKey')).toEqual({
+ type: 'DateTime',
+ name: 'dateKey',
+ canBeNullable: true,
+ });
+ });
+
+ it('should return \'int\' for integers', () => {
+ expect(getPrimitiveProp(42, 'numberKey')).toEqual({
+ type: 'int',
+ name: 'numberKey',
+ canBeNullable: true,
+ });
+ });
+
+ it('should return \'double\' for floating point numbers', () => {
+ expect(getPrimitiveProp(3.14, 'floatKey')).toEqual({
+ type: 'double',
+ name: 'floatKey',
+ canBeNullable: true,
+ });
+ });
+
+ it('should return \'bool\' for boolean values', () => {
+ expect(getPrimitiveProp(true, 'boolKey')).toEqual({
+ type: 'bool',
+ name: 'boolKey',
+ canBeNullable: true,
+ });
+ });
+
+ it('should throw an error for unsupported types', () => {
+ expect(() => getPrimitiveProp({}, 'objKey')).toThrow('Unexpected key \'objKey\' of type object');
+ });
+});
+
+// Test json2classes function
+describe('json2classes', () => {
+ it('should convert JSON to class definitions with custom root', () => {
+ const json = { name: 'John', age: 30 };
+ const result = json2classes(json, 'Main');
+ expect(result).to.deep.eq([
+ {
+ key: 'Main',
+ props: [
+ { type: 'string', name: 'name', canBeNullable: false },
+ { type: 'int', name: 'age', canBeNullable: true },
+ ],
+ },
+ ]);
+ });
+
+ it('should handle nested objects', () => {
+ const json = { user: { name: 'John' } };
+ const result = json2classes(json);
+ expect(result).to.deep.eq([
+ { key: 'Root', props: [{ type: 'User', name: 'user' }] },
+ { key: 'User', props: [{ type: 'string', name: 'name', canBeNullable: false }] },
+ ]);
+ });
+
+ it('should handle arrays of primitives', () => {
+ const json = { numbers: [1, 2, 3] };
+ const result = json2classes(json);
+ expect(result).to.deep.eq([
+ { key: 'Root', props: [{ type: 'int', name: 'numbers', isArray: true, canBeNullable: true }] },
+ ]);
+ });
+
+ it('should handle arrays of objects', () => {
+ const json = { users: [{ name: 'John' }, { name: 'Jane' }] };
+ const result = json2classes(json);
+ expect(result).to.deep.eq([
+ { key: 'Root', props: [{ type: 'UsersItem', name: 'users', isArray: true }] },
+ { key: 'UsersItem', props: [{ type: 'string', name: 'name', canBeNullable: false }] },
+ ]);
+ });
+});
+
+// Test json2csharp function
+describe('json2csharp', () => {
+ const cleanTabs = (s: string) => s.replace(/\t/g, ' ');
+
+ it('should convert JSON to C# class with default options', () => {
+ const json = JSON.stringify({ name: 'John', age: 30 });
+ const result = cleanTabs(json2csharp({ src: json }));
+
+ expect(result).toBe(`using System;
+using System.Text.Json;
+
+public class Root
+{
+ [JsonPropertyName(\"name\")]
+ public string Name { get; set; }
+ [JsonPropertyName(\"age\")]
+ public int Age { get; set; }
+}`);
+ });
+
+ it('should not change case if pascalCase option is false', () => {
+ const json = JSON.stringify({ test: { user_name: 'John' } });
+ const result = cleanTabs(json2csharp({ src: json, pascalCase: false }));
+
+ expect(result).toBe(`using System;
+using System.Text.Json;
+
+public class Root
+{
+ [JsonPropertyName(\"test\")]
+ public Test test { get; set; }
+}
+
+public class Test
+{
+ [JsonPropertyName(\"user_name\")]
+ public string user_name { get; set; }
+}`);
+ });
+
+ it('should handle when root is an array', () => {
+ const json = JSON.stringify([{ age: 30 }]);
+ const result = cleanTabs(json2csharp({ src: json, useNullable: true }));
+
+ expect(result).toBe(`using System;
+using System.Text.Json;
+
+public class RootItem
+{
+ [JsonPropertyName(\"age\")]
+ public int? Age { get; set; }
+}`);
+ });
+
+ it('should use nullable types if useNullable option is true', () => {
+ const json = JSON.stringify({ age: 30 });
+ const result = cleanTabs(json2csharp({ src: json, useNullable: true }));
+
+ expect(result).toBe(`using System;
+using System.Text.Json;
+
+public class Root
+{
+ [JsonPropertyName(\"age\")]
+ public int? Age { get; set; }
+}`);
+ });
+
+ it('should add JsonProperty attributes if addJsonProperty is true', () => {
+ const json = JSON.stringify({ name: 'John' });
+ const result = cleanTabs(json2csharp({ src: json, addJsonProperty: true }));
+
+ expect(result).toBe(`using System;
+using Newtonsoft.Json;
+using System.Text.Json;
+
+public class Root
+{
+ [JsonProperty(\"name\", NullValueHandling = NullValueHandling.Ignore)]
+ [JsonPropertyName(\"name\")]
+ public string Name { get; set; }
+}`);
+ });
+
+ it('should add JsonPropertyName attributes if addJsonPropertyName is true', () => {
+ const json = JSON.stringify({ name: 'John' });
+ const result = cleanTabs(json2csharp({ src: json, addJsonPropertyName: true }));
+
+ expect(result).toBe(`using System;
+using System.Text.Json;
+
+public class Root
+{
+ [JsonPropertyName(\"name\")]
+ public string Name { get; set; }
+}`);
+ });
+
+ it('should generate immutable classes if generateImmutableClasses is true', () => {
+ const json = JSON.stringify({ name: 'John' });
+ const result = cleanTabs(json2csharp({ src: json, generateImmutableClasses: true }));
+
+ expect(result).toBe(`using System;
+using System.Text.Json;
+
+public class Root
+{
+ public Root(
+ string name
+ ){
+ this.Name = name;
+ }
+
+ [JsonPropertyName(\"name\")]
+ public string Name { get; }
+}`);
+ });
+
+ it('should convert JSON with arrays into C# classes', () => {
+ const json = {
+ users: [{ name: 'John', age: 30 }, { name: 'Jane', age: 25 }],
+ };
+
+ expect(cleanTabs(json2csharp({ src: json }))).toBe(`using System;
+using System.Text.Json;
+
+public class Root
+{
+ [JsonPropertyName(\"users\")]
+ public UsersItem[] Users { get; set; }
+}
+
+public class UsersItem
+{
+ [JsonPropertyName(\"name\")]
+ public string Name { get; set; }
+ [JsonPropertyName(\"age\")]
+ public int Age { get; set; }
+}`);
+ expect(cleanTabs(json2csharp({ src: json, useRecordTypes: true }))).toBe(`using System;
+using System.Text.Json;
+
+public record Root(
+ [property: JsonPropertyName(\"users\")]
+ UsersItem[] Users
+);
+
+public record UsersItem(
+ [property: JsonPropertyName(\"name\")]
+ string Name,
+ [property: JsonPropertyName(\"age\")]
+ int Age
+);`);
+ expect(cleanTabs(json2csharp({ src: json, useRecordTypes: true, pascalCase: false, useReadonlyLists: true }))).toBe(`using System;
+using System.Text.Json;
+
+public record Root(
+ [property: JsonPropertyName(\"users\")]
+ IReadonlyList