diff --git a/DevDisciples.Json.Parser.Tests/JsonParserTests.cs b/DevDisciples.Json.Parser.Tests/JsonParserTests.cs index 5942408..5663ebe 100644 --- a/DevDisciples.Json.Parser.Tests/JsonParserTests.cs +++ b/DevDisciples.Json.Parser.Tests/JsonParserTests.cs @@ -86,8 +86,8 @@ public class JsonParserTests [ JsonNumber { Value: 1 }, JsonBool { Value: true }, - JsonObject { Properties: null}, - JsonArray { Elements: null }, + JsonObject { Properties.Length: 0 }, + JsonArray { Elements.Length: 0 }, JsonNull ] }); @@ -98,19 +98,19 @@ public class JsonParserTests { const string source = "{}"; var tokens = JsonParser.Parse(nameof(Can_parse_an_empty_object), source); - Assert.That(tokens is JsonObject { Properties: null }); + Assert.That(tokens is JsonObject { Properties.Length: 0 }); } [Test] public void Can_parse_an_object_with_one_entry() { - const string source = "{\"first_name\":\"John\nDoe\"}"; + const string source = "{\"first_name\":\"John\"}"; var node = JsonParser.Parse(nameof(Can_parse_an_object_with_one_entry), source); - Assert.That(node is JsonObject { Properties.Count: 1 }); + Assert.That(node is JsonObject { Properties.Length: 1 }); var @object = (JsonObject)node; - Assert.That(@object.Properties.ContainsKey("first_name")); - Assert.That(@object.Properties["first_name"] is JsonString { Value: "John" }); + Assert.That(@object.Properties.Any(property => property.Key == "first_name")); + Assert.That(@object.Properties.First(property => property.Key == "first_name").Value is JsonString { Value: "John" }); } [Test] @@ -121,12 +121,12 @@ public class JsonParserTests Assert.Multiple(() => { - Assert.That(node is JsonObject { Properties.Count: 2 }); + Assert.That(node is JsonObject { Properties.Length: 2 }); var @object = (JsonObject)node; - Assert.That(@object.Properties.ContainsKey("first_name")); - Assert.That(@object.Properties["first_name"] is JsonString { Value: "John" }); - Assert.That(@object.Properties.ContainsKey("last_name")); - Assert.That(@object.Properties["last_name"] is JsonString { Value: "Doe" }); + Assert.That(@object.Properties.Any(property => property.Key == "first_name")); + Assert.That(@object.Properties.First(property => property.Key == "first_name").Value is JsonString { Value: "John" }); + Assert.That(@object.Properties.Any(property => property.Key == "last_name")); + Assert.That(@object.Properties.First(property => property.Key == "last_name").Value is JsonString { Value: "Doe" }); }); } } \ No newline at end of file diff --git a/DevDisciples.Json.Parser/JsonArray.cs b/DevDisciples.Json.Parser/JsonArray.cs index 7ddbdd5..c534136 100644 --- a/DevDisciples.Json.Parser/JsonArray.cs +++ b/DevDisciples.Json.Parser/JsonArray.cs @@ -2,14 +2,14 @@ using DevDisciples.Parsing; namespace DevDisciples.Json.Parser; -public struct JsonArray : ISyntaxNode +public readonly struct JsonArray : ISyntaxNode { public Lexer.Token Token { get; } - public List Elements { get; } + public ISyntaxNode[] Elements { get; } - public JsonArray(Lexer.Token token, List? elements) + public JsonArray(Lexer.Token token, ISyntaxNode[] elements) { Token = token; - Elements = elements ?? new(); + Elements = elements; } } \ No newline at end of file diff --git a/DevDisciples.Json.Parser/JsonBool.cs b/DevDisciples.Json.Parser/JsonBool.cs index a7501ae..9dfff08 100644 --- a/DevDisciples.Json.Parser/JsonBool.cs +++ b/DevDisciples.Json.Parser/JsonBool.cs @@ -2,7 +2,7 @@ using DevDisciples.Parsing; namespace DevDisciples.Json.Parser; -public struct JsonBool : ISyntaxNode +public readonly struct JsonBool : ISyntaxNode { public Lexer.Token Token { get; } public bool Value => bool.TryParse(Token.Lexeme, out var val) && val; diff --git a/DevDisciples.Json.Parser/JsonNull.cs b/DevDisciples.Json.Parser/JsonNull.cs index ecd3610..fa662c3 100644 --- a/DevDisciples.Json.Parser/JsonNull.cs +++ b/DevDisciples.Json.Parser/JsonNull.cs @@ -2,7 +2,7 @@ using DevDisciples.Parsing; namespace DevDisciples.Json.Parser; -public struct JsonNull : ISyntaxNode +public readonly struct JsonNull : ISyntaxNode { public Lexer.Token Token { get; } diff --git a/DevDisciples.Json.Parser/JsonNumber.cs b/DevDisciples.Json.Parser/JsonNumber.cs index 89019c9..bc8e7cc 100644 --- a/DevDisciples.Json.Parser/JsonNumber.cs +++ b/DevDisciples.Json.Parser/JsonNumber.cs @@ -2,7 +2,7 @@ using DevDisciples.Parsing; namespace DevDisciples.Json.Parser; -public struct JsonNumber : ISyntaxNode +public readonly struct JsonNumber : ISyntaxNode { public Lexer.Token Token { get; } public double Value => double.TryParse(Token.Lexeme.Replace('.', ','), out var val) ? val : default; diff --git a/DevDisciples.Json.Parser/JsonObject.Property.cs b/DevDisciples.Json.Parser/JsonObject.Property.cs new file mode 100644 index 0000000..d6981f7 --- /dev/null +++ b/DevDisciples.Json.Parser/JsonObject.Property.cs @@ -0,0 +1,18 @@ +using DevDisciples.Parsing; + +namespace DevDisciples.Json.Parser; + +public readonly partial struct JsonObject +{ + public readonly struct Property : ISyntaxNode + { + public string Key { get; } + public ISyntaxNode Value { get; } + + public Property(string key, ISyntaxNode value) + { + Key = key; + Value = value; + } + } +} \ No newline at end of file diff --git a/DevDisciples.Json.Parser/JsonObject.cs b/DevDisciples.Json.Parser/JsonObject.cs index 6a68d6b..6b42d25 100644 --- a/DevDisciples.Json.Parser/JsonObject.cs +++ b/DevDisciples.Json.Parser/JsonObject.cs @@ -2,14 +2,14 @@ using DevDisciples.Parsing; namespace DevDisciples.Json.Parser; -public struct JsonObject : ISyntaxNode +public readonly partial struct JsonObject : ISyntaxNode { public Lexer.Token Token { get; } - public Dictionary Properties { get; set; } + public Property[] Properties { get; } - public JsonObject(Lexer.Token token, Dictionary? properties) + public JsonObject(Lexer.Token token, Property[] properties) { Token = token; - Properties = properties ?? new(); + Properties = properties; } } \ No newline at end of file diff --git a/DevDisciples.Json.Parser/JsonParser.cs b/DevDisciples.Json.Parser/JsonParser.cs index 0cc62f1..e9f8872 100644 --- a/DevDisciples.Json.Parser/JsonParser.cs +++ b/DevDisciples.Json.Parser/JsonParser.cs @@ -51,7 +51,7 @@ public static partial class JsonParser ctx.Consume(JsonToken.RightBracket, "Expected ']'"); - return new JsonArray(previous, elements); + return new JsonArray(previous, elements?.ToArray() ?? System.Array.Empty()); } private static ISyntaxNode Object(ParserContext ctx) @@ -72,7 +72,9 @@ public static partial class JsonParser ctx.Consume(JsonToken.RightBrace, "Expected '}'"); - return new JsonObject(previous, properties); + var propertiesArray = properties?.Select(kv => new JsonObject.Property(kv.Key, kv.Value)).ToArray(); + + return new JsonObject(previous, propertiesArray ?? System.Array.Empty()); } private static ISyntaxNode Number(ParserContext ctx) diff --git a/DevDisciples.Json.Parser/JsonString.cs b/DevDisciples.Json.Parser/JsonString.cs index 68c5cac..9541644 100644 --- a/DevDisciples.Json.Parser/JsonString.cs +++ b/DevDisciples.Json.Parser/JsonString.cs @@ -2,7 +2,7 @@ using DevDisciples.Parsing; namespace DevDisciples.Json.Parser; -public struct JsonString : ISyntaxNode +public readonly struct JsonString : ISyntaxNode { public Lexer.Token Token { get; } public string Value => Token.Lexeme; diff --git a/DevDisciples.Json.Tools.API/Controllers/TransformController.cs b/DevDisciples.Json.Tools.API/Controllers/TransformController.cs index 763662a..8578190 100644 --- a/DevDisciples.Json.Tools.API/Controllers/TransformController.cs +++ b/DevDisciples.Json.Tools.API/Controllers/TransformController.cs @@ -1,43 +1,43 @@ -using DevDisciples.Json.Tools.API.Requests; -using Microsoft.AspNetCore.Mvc; - -namespace DevDisciples.Json.Tools.API.Controllers; - -[ApiController] -[Route("api/[controller]")] -public class TransformController : ControllerBase -{ - [HttpPost("uglify")] - public IActionResult Uglify([FromBody] UglifyRequest request) - { - var context = new JsonFormatter.Context { Beautify = false }; - - var result = JsonFormatter.Format(request.Source, context); - - return Ok(new { Result = result }); - } - - [HttpPost("prettify")] - public IActionResult Prettify([FromBody] PrettifyRequest request) - { - var context = new JsonFormatter.Context { Beautify = true, IndentSize = request.IndentSize }; - - var result = JsonFormatter.Format(request.Source, context); - - return Ok(new { Result = result }); - } - - [HttpPost("json2csharp")] - public IActionResult Json2CSharp([FromBody] Json2CsharpRequest request) - { - var context = new Json2CSharpTranslator.Context - { - RootClassName = request.RootClassName, - Namespace = request.Namespace, - }; - - var result = Json2CSharpTranslator.Translate(request.Source, context); - - return Ok(new { Result = result }); - } +using DevDisciples.Json.Tools.API.Requests; +using Microsoft.AspNetCore.Mvc; + +namespace DevDisciples.Json.Tools.API.Controllers; + +[ApiController] +[Route("api/[controller]")] +public class TransformController : ControllerBase +{ + [HttpPost("uglify")] + public IActionResult Uglify([FromBody] UglifyRequest request) + { + var context = new JsonFormatter.Context { Beautify = false }; + + var result = JsonFormatter.Format(request.Source, context); + + return Ok(new { Result = result }); + } + + [HttpPost("prettify")] + public IActionResult Prettify([FromBody] PrettifyRequest request) + { + var context = new JsonFormatter.Context { Beautify = true, IndentSize = request.IndentSize }; + + var result = JsonFormatter.Format(request.Source, context); + + return Ok(new { Result = result }); + } + + [HttpPost("json2csharp")] + public IActionResult Json2CSharp([FromBody] Json2CsharpRequest request) + { + var context = new Json2CSharpTranslator.Context + { + RootClassName = request.RootClassName, + Namespace = request.Namespace, + }; + + var result = Json2CSharpTranslator.Translate(request.Source, context); + + return Ok(new { Result = result }); + } } \ No newline at end of file diff --git a/DevDisciples.Json.Tools.API/GlobalExceptionHandler.cs b/DevDisciples.Json.Tools.API/GlobalExceptionHandler.cs index cd501fd..0a54070 100644 --- a/DevDisciples.Json.Tools.API/GlobalExceptionHandler.cs +++ b/DevDisciples.Json.Tools.API/GlobalExceptionHandler.cs @@ -1,46 +1,46 @@ -using DevDisciples.Parsing; -using Microsoft.AspNetCore.Diagnostics; -using Microsoft.AspNetCore.Mvc; - -namespace DevDisciples.Json.Tools.API; - -public class GlobalExceptionHandler : IExceptionHandler -{ - public const int ParsingExceptionStatusCode = 499; - - private readonly ILogger _logger; - - public GlobalExceptionHandler(ILogger logger) - { - _logger = logger; - } - - public async ValueTask TryHandleAsync( - HttpContext httpContext, - Exception exception, - CancellationToken cancellationToken - ) - { - switch (exception) - { - case ParsingException: - _logger.LogError(exception, "An exception occurred: {Message}", exception.Message); - - var problem = new ProblemDetails - { - Status = ParsingExceptionStatusCode, - Title = "Parsing Exception", - Detail = exception.Message - }; - - httpContext.Response.StatusCode = problem.Status.Value; - - await httpContext.Response.WriteAsJsonAsync(problem, cancellationToken); - - return true; - - default: - return false; - } - } +using DevDisciples.Parsing; +using Microsoft.AspNetCore.Diagnostics; +using Microsoft.AspNetCore.Mvc; + +namespace DevDisciples.Json.Tools.API; + +public class GlobalExceptionHandler : IExceptionHandler +{ + public const int ParsingExceptionStatusCode = 499; + + private readonly ILogger _logger; + + public GlobalExceptionHandler(ILogger logger) + { + _logger = logger; + } + + public async ValueTask TryHandleAsync( + HttpContext httpContext, + Exception exception, + CancellationToken cancellationToken + ) + { + switch (exception) + { + case ParsingException: + _logger.LogError(exception, "An exception occurred: {Message}", exception.Message); + + var problem = new ProblemDetails + { + Status = ParsingExceptionStatusCode, + Title = "Parsing Exception", + Detail = exception.Message + }; + + httpContext.Response.StatusCode = problem.Status.Value; + + await httpContext.Response.WriteAsJsonAsync(problem, cancellationToken); + + return true; + + default: + return false; + } + } } \ No newline at end of file diff --git a/DevDisciples.Json.Tools.API/Requests/Json2CsharpRequest.cs b/DevDisciples.Json.Tools.API/Requests/Json2CsharpRequest.cs index bb142f1..96dee83 100644 --- a/DevDisciples.Json.Tools.API/Requests/Json2CsharpRequest.cs +++ b/DevDisciples.Json.Tools.API/Requests/Json2CsharpRequest.cs @@ -1,7 +1,7 @@ -namespace DevDisciples.Json.Tools.API.Requests; - -public class Json2CsharpRequest : TransformRequest -{ - public string RootClassName { get; set; } = Json2CSharpTranslator.Context.DefaultRootClassName; - public string Namespace { get; set; } = Json2CSharpTranslator.Context.DefaultNamespace; +namespace DevDisciples.Json.Tools.API.Requests; + +public class Json2CsharpRequest : TransformRequest +{ + public string RootClassName { get; } = Json2CSharpTranslator.Context.DefaultRootClassName; + public string Namespace { get; } = Json2CSharpTranslator.Context.DefaultNamespace; } \ No newline at end of file diff --git a/DevDisciples.Json.Tools.API/Requests/PrettifyRequest.cs b/DevDisciples.Json.Tools.API/Requests/PrettifyRequest.cs index 048f89d..f973d0f 100644 --- a/DevDisciples.Json.Tools.API/Requests/PrettifyRequest.cs +++ b/DevDisciples.Json.Tools.API/Requests/PrettifyRequest.cs @@ -1,6 +1,6 @@ -namespace DevDisciples.Json.Tools.API.Requests; - -public class PrettifyRequest : TransformRequest -{ - public int IndentSize { get; set; } = JsonFormatter.Context.DefaultIndentSize; +namespace DevDisciples.Json.Tools.API.Requests; + +public class PrettifyRequest : TransformRequest +{ + public int IndentSize { get; } = JsonFormatter.Context.DefaultIndentSize; } \ No newline at end of file diff --git a/DevDisciples.Json.Tools.API/Requests/TransformRequest.cs b/DevDisciples.Json.Tools.API/Requests/TransformRequest.cs index eec464e..dcae188 100644 --- a/DevDisciples.Json.Tools.API/Requests/TransformRequest.cs +++ b/DevDisciples.Json.Tools.API/Requests/TransformRequest.cs @@ -1,6 +1,6 @@ -namespace DevDisciples.Json.Tools.API.Requests; - -public abstract class TransformRequest -{ - public string Source { get; set; } = string.Empty; +namespace DevDisciples.Json.Tools.API.Requests; + +public abstract class TransformRequest +{ + public string Source { get; init; } = string.Empty; } \ No newline at end of file diff --git a/DevDisciples.Json.Tools.API/Requests/UglifyRequest.cs b/DevDisciples.Json.Tools.API/Requests/UglifyRequest.cs index f581428..fc1ef5e 100644 --- a/DevDisciples.Json.Tools.API/Requests/UglifyRequest.cs +++ b/DevDisciples.Json.Tools.API/Requests/UglifyRequest.cs @@ -1,5 +1,5 @@ -namespace DevDisciples.Json.Tools.API.Requests; - -public class UglifyRequest : TransformRequest -{ +namespace DevDisciples.Json.Tools.API.Requests; + +public class UglifyRequest : TransformRequest +{ } \ No newline at end of file diff --git a/DevDisciples.Json.Tools.App/src/app/home/home.component.html b/DevDisciples.Json.Tools.App/src/app/home/home.component.html index 5f2c53f..6e1756e 100644 --- a/DevDisciples.Json.Tools.App/src/app/home/home.component.html +++ b/DevDisciples.Json.Tools.App/src/app/home/home.component.html @@ -1 +1 @@ -

home works!

+

home works!

diff --git a/DevDisciples.Json.Tools.App/src/app/home/home.component.spec.ts b/DevDisciples.Json.Tools.App/src/app/home/home.component.spec.ts index 1191557..f94d44e 100644 --- a/DevDisciples.Json.Tools.App/src/app/home/home.component.spec.ts +++ b/DevDisciples.Json.Tools.App/src/app/home/home.component.spec.ts @@ -1,23 +1,23 @@ -import { ComponentFixture, TestBed } from '@angular/core/testing'; - -import { HomeComponent } from './home.component'; - -describe('HomeComponent', () => { - let component: HomeComponent; - let fixture: ComponentFixture; - - beforeEach(async () => { - await TestBed.configureTestingModule({ - imports: [HomeComponent] - }) - .compileComponents(); - - fixture = TestBed.createComponent(HomeComponent); - component = fixture.componentInstance; - fixture.detectChanges(); - }); - - it('should create', () => { - expect(component).toBeTruthy(); - }); -}); +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { HomeComponent } from './home.component'; + +describe('HomeComponent', () => { + let component: HomeComponent; + let fixture: ComponentFixture; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + imports: [HomeComponent] + }) + .compileComponents(); + + fixture = TestBed.createComponent(HomeComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/DevDisciples.Json.Tools.App/src/app/home/home.component.ts b/DevDisciples.Json.Tools.App/src/app/home/home.component.ts index deb69c4..95b4d90 100644 --- a/DevDisciples.Json.Tools.App/src/app/home/home.component.ts +++ b/DevDisciples.Json.Tools.App/src/app/home/home.component.ts @@ -1,12 +1,12 @@ -import { Component } from '@angular/core'; - -@Component({ - selector: 'app-home', - standalone: true, - imports: [], - templateUrl: './home.component.html', - styleUrl: './home.component.scss' -}) -export class HomeComponent { - -} +import { Component } from '@angular/core'; + +@Component({ + selector: 'app-home', + standalone: true, + imports: [], + templateUrl: './home.component.html', + styleUrl: './home.component.scss' +}) +export class HomeComponent { + +} diff --git a/DevDisciples.Json.Tools.App/src/app/input-output/input-output.component.html b/DevDisciples.Json.Tools.App/src/app/input-output/input-output.component.html index 85f6505..13d8e6d 100644 --- a/DevDisciples.Json.Tools.App/src/app/input-output/input-output.component.html +++ b/DevDisciples.Json.Tools.App/src/app/input-output/input-output.component.html @@ -1,15 +1,15 @@ -
-
-

Input

- - -
- -
+
+
+

Input

+ + +
+ +
diff --git a/DevDisciples.Json.Tools.App/src/app/input-output/input-output.component.scss b/DevDisciples.Json.Tools.App/src/app/input-output/input-output.component.scss index 89cc6c0..f7d3e63 100644 --- a/DevDisciples.Json.Tools.App/src/app/input-output/input-output.component.scss +++ b/DevDisciples.Json.Tools.App/src/app/input-output/input-output.component.scss @@ -1,12 +1,12 @@ -main { - display: flex; - flex-direction: row; - - #left { - flex: .5; - } - - #right { - flex: .5; - } -} +main { + display: flex; + flex-direction: row; + + #left { + flex: .5; + } + + #right { + flex: .5; + } +} diff --git a/DevDisciples.Json.Tools.App/src/app/input-output/input-output.component.spec.ts b/DevDisciples.Json.Tools.App/src/app/input-output/input-output.component.spec.ts index 7d8a9e0..cca9c9c 100644 --- a/DevDisciples.Json.Tools.App/src/app/input-output/input-output.component.spec.ts +++ b/DevDisciples.Json.Tools.App/src/app/input-output/input-output.component.spec.ts @@ -1,23 +1,23 @@ -import { ComponentFixture, TestBed } from '@angular/core/testing'; - -import { InputOutputComponent } from './input-output.component'; - -describe('InputOutputComponent', () => { - let component: InputOutputComponent; - let fixture: ComponentFixture; - - beforeEach(async () => { - await TestBed.configureTestingModule({ - imports: [InputOutputComponent] - }) - .compileComponents(); - - fixture = TestBed.createComponent(InputOutputComponent); - component = fixture.componentInstance; - fixture.detectChanges(); - }); - - it('should create', () => { - expect(component).toBeTruthy(); - }); -}); +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { InputOutputComponent } from './input-output.component'; + +describe('InputOutputComponent', () => { + let component: InputOutputComponent; + let fixture: ComponentFixture; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + imports: [InputOutputComponent] + }) + .compileComponents(); + + fixture = TestBed.createComponent(InputOutputComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/DevDisciples.Json.Tools.App/src/app/input-output/input-output.component.ts b/DevDisciples.Json.Tools.App/src/app/input-output/input-output.component.ts index 82caa5d..e3390a3 100644 --- a/DevDisciples.Json.Tools.App/src/app/input-output/input-output.component.ts +++ b/DevDisciples.Json.Tools.App/src/app/input-output/input-output.component.ts @@ -1,25 +1,25 @@ -import {Component, EventEmitter, Input, Output} from '@angular/core'; -import {EditorComponent} from "ngx-monaco-editor-v2"; -import {FormsModule} from "@angular/forms"; - -@Component({ - selector: 'app-input-output', - standalone: true, - imports: [ - EditorComponent, - FormsModule - ], - templateUrl: './input-output.component.html', - styleUrl: './input-output.component.scss' -}) -export class InputOutputComponent { - @Input() input!: string; - @Input() inputOptions!: any; - @Output() onInputChange = new EventEmitter(); - @Input() output!: string; - @Input() outputOptions!: any; - - handleInputChange($event: string) { - this.onInputChange.emit($event); - } -} +import {Component, EventEmitter, Input, Output} from '@angular/core'; +import {EditorComponent} from "ngx-monaco-editor-v2"; +import {FormsModule} from "@angular/forms"; + +@Component({ + selector: 'app-input-output', + standalone: true, + imports: [ + EditorComponent, + FormsModule + ], + templateUrl: './input-output.component.html', + styleUrl: './input-output.component.scss' +}) +export class InputOutputComponent { + @Input() input!: string; + @Input() inputOptions!: any; + @Output() onInputChange = new EventEmitter(); + @Input() output!: string; + @Input() outputOptions!: any; + + handleInputChange($event: string) { + this.onInputChange.emit($event); + } +} diff --git a/DevDisciples.Json.Tools.App/src/app/json-transform.service.spec.ts b/DevDisciples.Json.Tools.App/src/app/json-transform.service.spec.ts index 0c206e2..9a2b90f 100644 --- a/DevDisciples.Json.Tools.App/src/app/json-transform.service.spec.ts +++ b/DevDisciples.Json.Tools.App/src/app/json-transform.service.spec.ts @@ -1,16 +1,16 @@ -import { TestBed } from '@angular/core/testing'; - -import { JsonTransformService } from './json-transform.service'; - -describe('JsonTransformService', () => { - let service: JsonTransformService; - - beforeEach(() => { - TestBed.configureTestingModule({}); - service = TestBed.inject(JsonTransformService); - }); - - it('should be created', () => { - expect(service).toBeTruthy(); - }); -}); +import { TestBed } from '@angular/core/testing'; + +import { JsonTransformService } from './json-transform.service'; + +describe('JsonTransformService', () => { + let service: JsonTransformService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(JsonTransformService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/DevDisciples.Json.Tools.App/src/app/json-transform.service.ts b/DevDisciples.Json.Tools.App/src/app/json-transform.service.ts index b23020a..35d86a6 100644 --- a/DevDisciples.Json.Tools.App/src/app/json-transform.service.ts +++ b/DevDisciples.Json.Tools.App/src/app/json-transform.service.ts @@ -1,25 +1,25 @@ -import {Injectable} from '@angular/core'; -import {HttpClient, HttpResponse} from "@angular/common/http"; -import {Observable} from "rxjs"; - -@Injectable({ - providedIn: 'root' -}) -export class JsonTransformService { - private url: string = "https://localhost:5001"; - - constructor(private http: HttpClient) { - } - - public prettify(source: string): Observable> { - return this.http.post(`${this.url}/api/transform/prettify`, {source: source}, {observe: "response"}); - } - - public uglify(source: string): Observable> { - return this.http.post(`${this.url}/api/transform/uglify`, {source: source}, {observe: "response"}); - } - - public json2csharp(source: string): Observable> { - return this.http.post(`${this.url}/api/transform/json2csharp`, {source: source}, {observe: "response"}); - } -} +import {Injectable} from '@angular/core'; +import {HttpClient, HttpResponse} from "@angular/common/http"; +import {Observable} from "rxjs"; + +@Injectable({ + providedIn: 'root' +}) +export class JsonTransformService { + private url: string = "https://localhost:5001"; + + constructor(private http: HttpClient) { + } + + public prettify(source: string): Observable> { + return this.http.post(`${this.url}/api/transform/prettify`, {Source: source}, {observe: "response"}); + } + + public uglify(source: string): Observable> { + return this.http.post(`${this.url}/api/transform/uglify`, {Source: source}, {observe: "response"}); + } + + public json2csharp(source: string): Observable> { + return this.http.post(`${this.url}/api/transform/json2csharp`, {Source: source}, {observe: "response"}); + } +} diff --git a/DevDisciples.Json.Tools.App/src/app/json2csharp/json2-csharp.component.html b/DevDisciples.Json.Tools.App/src/app/json2csharp/json2-csharp.component.html index f04307e..671f43c 100644 --- a/DevDisciples.Json.Tools.App/src/app/json2csharp/json2-csharp.component.html +++ b/DevDisciples.Json.Tools.App/src/app/json2csharp/json2-csharp.component.html @@ -1,8 +1,8 @@ -

JSON to C#

- - - +

JSON to C#

+ + + diff --git a/DevDisciples.Json.Tools.App/src/app/json2csharp/json2-csharp.component.spec.ts b/DevDisciples.Json.Tools.App/src/app/json2csharp/json2-csharp.component.spec.ts index 8448b17..a06d423 100644 --- a/DevDisciples.Json.Tools.App/src/app/json2csharp/json2-csharp.component.spec.ts +++ b/DevDisciples.Json.Tools.App/src/app/json2csharp/json2-csharp.component.spec.ts @@ -1,23 +1,23 @@ -import { ComponentFixture, TestBed } from '@angular/core/testing'; - -import { Json2CsharpComponent } from './json2-csharp.component'; - -describe('Json2csharpComponent', () => { - let component: Json2CsharpComponent; - let fixture: ComponentFixture; - - beforeEach(async () => { - await TestBed.configureTestingModule({ - imports: [Json2CsharpComponent] - }) - .compileComponents(); - - fixture = TestBed.createComponent(Json2CsharpComponent); - component = fixture.componentInstance; - fixture.detectChanges(); - }); - - it('should create', () => { - expect(component).toBeTruthy(); - }); -}); +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { Json2CsharpComponent } from './json2-csharp.component'; + +describe('Json2csharpComponent', () => { + let component: Json2CsharpComponent; + let fixture: ComponentFixture; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + imports: [Json2CsharpComponent] + }) + .compileComponents(); + + fixture = TestBed.createComponent(Json2CsharpComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/DevDisciples.Json.Tools.App/src/app/json2csharp/json2-csharp.component.ts b/DevDisciples.Json.Tools.App/src/app/json2csharp/json2-csharp.component.ts index 4deffc8..d46e6d8 100644 --- a/DevDisciples.Json.Tools.App/src/app/json2csharp/json2-csharp.component.ts +++ b/DevDisciples.Json.Tools.App/src/app/json2csharp/json2-csharp.component.ts @@ -1,55 +1,69 @@ -import {Component, OnInit} from '@angular/core'; -import {InputOutputComponent} from "../input-output/input-output.component"; -import {JsonTransformService} from "../json-transform.service"; - -@Component({ - selector: 'app-json2csharp', - standalone: true, - imports: [ - InputOutputComponent - ], - templateUrl: './json2-csharp.component.html', - styleUrl: './json2-csharp.component.scss' -}) -export class Json2CsharpComponent implements OnInit { - input: string = JSON.stringify({first_name: "John", last_name: "Doe"}, null, 2); - inputOptions = {theme: 'vs-dark', language: 'json', readOnly: false}; - output: string = JSON.stringify({first_name: "John", last_name: "Doe"}); - outputOptions = {theme: 'vs-dark', language: 'csharp', readOnly: true}; - - constructor(private service: JsonTransformService) { - } - - ngOnInit(): void { - this.service - .json2csharp(this.input) - .subscribe(response => { - console.log(response); - - if (response.status === 200) { - this.output = response.body.result; - } else if (response.status === 499) { - this.output = response.body.detail; - } - }); - } - - handleInputChange($event: any) { - console.log($event); - this.service - .json2csharp($event) - .subscribe({ - next: response => { - console.log(response); - this.output = response.body.result; - }, - error: response => { - console.log(response) - if (response.status === 499) { - this.output = response.error.detail; - console.log(response.error.detail); - } - } - }); - } -} +import {AfterViewInit, Component, OnInit} from '@angular/core'; +import {InputOutputComponent} from "../input-output/input-output.component"; +import {JsonTransformService} from "../json-transform.service"; + +@Component({ + selector: 'app-json2csharp', + standalone: true, + imports: [ + InputOutputComponent + ], + templateUrl: './json2-csharp.component.html', + styleUrl: './json2-csharp.component.scss' +}) +export class Json2CsharpComponent implements AfterViewInit { + input: string = JSON.stringify({first_name: "John", last_name: "Doe"}, null, 2); + inputOptions = {theme: 'vs-dark', language: 'json', readOnly: false}; + output: string = JSON.stringify({first_name: "John", last_name: "Doe"}); + outputOptions = {theme: 'vs-dark', language: 'csharp', readOnly: true}; + + constructor(private service: JsonTransformService) { + } + + ngAfterViewInit(): void { + this.service + .json2csharp(this.input) + .subscribe(response => { + console.log(response); + + if (response.status === 200) { + this.output = response.body.result; + } else if (response.status === 499) { + this.output = response.body.detail; + } + }); + } + + ngOnInit(): void { + // this.service + // .json2csharp(this.input) + // .subscribe(response => { + // console.log(response); + // + // if (response.status === 200) { + // this.output = response.body.result; + // } else if (response.status === 499) { + // this.output = response.body.detail; + // } + // }); + } + + handleInputChange($event: any) { + console.log($event); + this.service + .json2csharp($event) + .subscribe({ + next: response => { + console.log(response); + this.output = response.body.result; + }, + error: response => { + console.log(response) + if (response.status === 499) { + this.output = response.error.detail; + console.log(response.error.detail); + } + } + }); + } +} diff --git a/DevDisciples.Json.Tools.App/src/app/layout/layout.component.html b/DevDisciples.Json.Tools.App/src/app/layout/layout.component.html index a172ab4..8e59c5f 100644 --- a/DevDisciples.Json.Tools.App/src/app/layout/layout.component.html +++ b/DevDisciples.Json.Tools.App/src/app/layout/layout.component.html @@ -1,25 +1,25 @@ - - - Menu - - Home - Prettify - Uglify - JSON to C# - - - - - - - JSON Transform - - -
- - -
-
-
+ + + Menu + + Home + Prettify + Uglify + JSON to C# + + + + + + + JSON Transform + + +
+ + +
+
+
diff --git a/DevDisciples.Json.Tools.App/src/app/layout/layout.component.scss b/DevDisciples.Json.Tools.App/src/app/layout/layout.component.scss index fbe4695..7d04f2a 100644 --- a/DevDisciples.Json.Tools.App/src/app/layout/layout.component.scss +++ b/DevDisciples.Json.Tools.App/src/app/layout/layout.component.scss @@ -1,11 +1,11 @@ -.sidenav-container { - height: 100%; -} - -.sidenav { - width: 250px; -} - -.content { - padding: 16px; -} +.sidenav-container { + height: 100%; +} + +.sidenav { + width: 250px; +} + +.content { + padding: 16px; +} diff --git a/DevDisciples.Json.Tools.App/src/app/layout/layout.component.spec.ts b/DevDisciples.Json.Tools.App/src/app/layout/layout.component.spec.ts index 09aa9fc..2528c75 100644 --- a/DevDisciples.Json.Tools.App/src/app/layout/layout.component.spec.ts +++ b/DevDisciples.Json.Tools.App/src/app/layout/layout.component.spec.ts @@ -1,23 +1,23 @@ -import { ComponentFixture, TestBed } from '@angular/core/testing'; - -import { LayoutComponent } from './layout.component'; - -describe('LayoutComponent', () => { - let component: LayoutComponent; - let fixture: ComponentFixture; - - beforeEach(async () => { - await TestBed.configureTestingModule({ - imports: [LayoutComponent] - }) - .compileComponents(); - - fixture = TestBed.createComponent(LayoutComponent); - component = fixture.componentInstance; - fixture.detectChanges(); - }); - - it('should create', () => { - expect(component).toBeTruthy(); - }); -}); +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { LayoutComponent } from './layout.component'; + +describe('LayoutComponent', () => { + let component: LayoutComponent; + let fixture: ComponentFixture; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + imports: [LayoutComponent] + }) + .compileComponents(); + + fixture = TestBed.createComponent(LayoutComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/DevDisciples.Json.Tools.App/src/app/layout/layout.component.ts b/DevDisciples.Json.Tools.App/src/app/layout/layout.component.ts index 128bd70..58604d2 100644 --- a/DevDisciples.Json.Tools.App/src/app/layout/layout.component.ts +++ b/DevDisciples.Json.Tools.App/src/app/layout/layout.component.ts @@ -1,27 +1,27 @@ -import { Component } from '@angular/core'; -import {MatSidenavContainer, MatSidenavModule} from "@angular/material/sidenav"; -import {MatToolbarModule} from "@angular/material/toolbar"; -import {MatListItem, MatNavList} from "@angular/material/list"; -import {RouterLink, RouterOutlet} from "@angular/router"; -import {MatIconButton} from "@angular/material/button"; -import {MatIconModule} from "@angular/material/icon"; - -@Component({ - selector: 'app-layout', - standalone: true, - imports: [ - MatSidenavContainer, - MatToolbarModule, - MatNavList, - MatListItem, - MatSidenavModule, - RouterLink, - MatIconButton, - MatIconModule, - RouterOutlet - ], - templateUrl: './layout.component.html', - styleUrl: './layout.component.scss' -}) -export class LayoutComponent { -} +import { Component } from '@angular/core'; +import {MatSidenavContainer, MatSidenavModule} from "@angular/material/sidenav"; +import {MatToolbarModule} from "@angular/material/toolbar"; +import {MatListItem, MatNavList} from "@angular/material/list"; +import {RouterLink, RouterOutlet} from "@angular/router"; +import {MatIconButton} from "@angular/material/button"; +import {MatIconModule} from "@angular/material/icon"; + +@Component({ + selector: 'app-layout', + standalone: true, + imports: [ + MatSidenavContainer, + MatToolbarModule, + MatNavList, + MatListItem, + MatSidenavModule, + RouterLink, + MatIconButton, + MatIconModule, + RouterOutlet + ], + templateUrl: './layout.component.html', + styleUrl: './layout.component.scss' +}) +export class LayoutComponent { +} diff --git a/DevDisciples.Json.Tools.App/src/app/prettify/prettify.component.html b/DevDisciples.Json.Tools.App/src/app/prettify/prettify.component.html index 5dc08cb..0695bab 100644 --- a/DevDisciples.Json.Tools.App/src/app/prettify/prettify.component.html +++ b/DevDisciples.Json.Tools.App/src/app/prettify/prettify.component.html @@ -1,8 +1,8 @@ -

JSON Prettify

- - - +

JSON Prettify

+ + + diff --git a/DevDisciples.Json.Tools.App/src/app/prettify/prettify.component.spec.ts b/DevDisciples.Json.Tools.App/src/app/prettify/prettify.component.spec.ts index 004cdf1..4cbd63a 100644 --- a/DevDisciples.Json.Tools.App/src/app/prettify/prettify.component.spec.ts +++ b/DevDisciples.Json.Tools.App/src/app/prettify/prettify.component.spec.ts @@ -1,23 +1,23 @@ -import { ComponentFixture, TestBed } from '@angular/core/testing'; - -import { PrettifyComponent } from './prettify.component'; - -describe('PrettifyComponent', () => { - let component: PrettifyComponent; - let fixture: ComponentFixture; - - beforeEach(async () => { - await TestBed.configureTestingModule({ - imports: [PrettifyComponent] - }) - .compileComponents(); - - fixture = TestBed.createComponent(PrettifyComponent); - component = fixture.componentInstance; - fixture.detectChanges(); - }); - - it('should create', () => { - expect(component).toBeTruthy(); - }); -}); +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { PrettifyComponent } from './prettify.component'; + +describe('PrettifyComponent', () => { + let component: PrettifyComponent; + let fixture: ComponentFixture; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + imports: [PrettifyComponent] + }) + .compileComponents(); + + fixture = TestBed.createComponent(PrettifyComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/DevDisciples.Json.Tools.App/src/app/prettify/prettify.component.ts b/DevDisciples.Json.Tools.App/src/app/prettify/prettify.component.ts index 89e71ab..9e7a897 100644 --- a/DevDisciples.Json.Tools.App/src/app/prettify/prettify.component.ts +++ b/DevDisciples.Json.Tools.App/src/app/prettify/prettify.component.ts @@ -1,46 +1,46 @@ -import {Component} from '@angular/core'; -import {EditorComponent} from "ngx-monaco-editor-v2"; -import {FormsModule} from "@angular/forms"; -import {InputOutputComponent} from "../input-output/input-output.component"; -import {JsonTransformService} from "../json-transform.service"; - -@Component({ - selector: 'app-prettify', - standalone: true, - imports: [ - EditorComponent, - FormsModule, - InputOutputComponent - ], - templateUrl: './prettify.component.html', - styleUrl: './prettify.component.scss' -}) -export class PrettifyComponent { - input: string = JSON.stringify({first_name: "John", last_name: "Doe"}); - inputOptions = {theme: 'vs-dark', language: 'json', readOnly: false}; - output: string = JSON.stringify({first_name: "John", last_name: "Doe"}, null, 2); - outputOptions = {theme: 'vs-dark', language: 'json', readOnly: true}; - error: string = ""; - - constructor(private service: JsonTransformService) { - } - - handleInputChange($event: any) { - console.log($event); - this.service - .prettify($event) - .subscribe({ - next: response => { - console.log(response); - this.output = response.body.result; - }, - error: response => { - console.log(response) - if (response.status === 499) { - this.output = response.error.detail; - console.log(response.error.detail); - } - } - }); - } -} +import {Component} from '@angular/core'; +import {EditorComponent} from "ngx-monaco-editor-v2"; +import {FormsModule} from "@angular/forms"; +import {InputOutputComponent} from "../input-output/input-output.component"; +import {JsonTransformService} from "../json-transform.service"; + +@Component({ + selector: 'app-prettify', + standalone: true, + imports: [ + EditorComponent, + FormsModule, + InputOutputComponent + ], + templateUrl: './prettify.component.html', + styleUrl: './prettify.component.scss' +}) +export class PrettifyComponent { + input: string = JSON.stringify({first_name: "John", last_name: "Doe"}); + inputOptions = {theme: 'vs-dark', language: 'json', readOnly: false}; + output: string = JSON.stringify({first_name: "John", last_name: "Doe"}, null, 2); + outputOptions = {theme: 'vs-dark', language: 'json', readOnly: true}; + error: string = ""; + + constructor(private service: JsonTransformService) { + } + + handleInputChange($event: any) { + console.log($event); + this.service + .prettify($event) + .subscribe({ + next: response => { + console.log(response); + this.output = response.body.result; + }, + error: response => { + console.log(response) + if (response.status === 499) { + this.output = response.error.detail; + console.log(response.error.detail); + } + } + }); + } +} diff --git a/DevDisciples.Json.Tools.App/src/app/uglify/uglify.component.html b/DevDisciples.Json.Tools.App/src/app/uglify/uglify.component.html index 578a3b4..deda139 100644 --- a/DevDisciples.Json.Tools.App/src/app/uglify/uglify.component.html +++ b/DevDisciples.Json.Tools.App/src/app/uglify/uglify.component.html @@ -1,8 +1,8 @@ -

JSON Uglify

- - - +

JSON Uglify

+ + + diff --git a/DevDisciples.Json.Tools.App/src/app/uglify/uglify.component.spec.ts b/DevDisciples.Json.Tools.App/src/app/uglify/uglify.component.spec.ts index 8d85bfa..f2950a8 100644 --- a/DevDisciples.Json.Tools.App/src/app/uglify/uglify.component.spec.ts +++ b/DevDisciples.Json.Tools.App/src/app/uglify/uglify.component.spec.ts @@ -1,23 +1,23 @@ -import { ComponentFixture, TestBed } from '@angular/core/testing'; - -import { UglifyComponent } from './uglify.component'; - -describe('UglifyComponent', () => { - let component: UglifyComponent; - let fixture: ComponentFixture; - - beforeEach(async () => { - await TestBed.configureTestingModule({ - imports: [UglifyComponent] - }) - .compileComponents(); - - fixture = TestBed.createComponent(UglifyComponent); - component = fixture.componentInstance; - fixture.detectChanges(); - }); - - it('should create', () => { - expect(component).toBeTruthy(); - }); -}); +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { UglifyComponent } from './uglify.component'; + +describe('UglifyComponent', () => { + let component: UglifyComponent; + let fixture: ComponentFixture; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + imports: [UglifyComponent] + }) + .compileComponents(); + + fixture = TestBed.createComponent(UglifyComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/DevDisciples.Json.Tools.App/src/app/uglify/uglify.component.ts b/DevDisciples.Json.Tools.App/src/app/uglify/uglify.component.ts index c1f24d8..b68b785 100644 --- a/DevDisciples.Json.Tools.App/src/app/uglify/uglify.component.ts +++ b/DevDisciples.Json.Tools.App/src/app/uglify/uglify.component.ts @@ -1,41 +1,41 @@ -import {Component} from '@angular/core'; -import {JsonTransformService} from "../json-transform.service"; -import {InputOutputComponent} from "../input-output/input-output.component"; - -@Component({ - selector: 'app-uglify', - standalone: true, - imports: [ - InputOutputComponent - ], - templateUrl: './uglify.component.html', - styleUrl: './uglify.component.scss' -}) -export class UglifyComponent { - input: string = JSON.stringify({first_name: "John", last_name: "Doe"}, null, 2); - inputOptions = {theme: 'vs-dark', language: 'json', readOnly: false}; - output: string = JSON.stringify({first_name: "John", last_name: "Doe"}); - outputOptions = {theme: 'vs-dark', language: 'json', readOnly: true}; - - constructor(private service: JsonTransformService) { - } - - handleInputChange($event: any) { - console.log($event); - this.service - .uglify($event) - .subscribe({ - next: response => { - console.log(response); - this.output = response.body.result; - }, - error: response => { - console.log(response) - if (response.status === 499) { - this.output = response.error.detail; - console.log(response.error.detail); - } - } - }); - } -} +import {Component} from '@angular/core'; +import {JsonTransformService} from "../json-transform.service"; +import {InputOutputComponent} from "../input-output/input-output.component"; + +@Component({ + selector: 'app-uglify', + standalone: true, + imports: [ + InputOutputComponent + ], + templateUrl: './uglify.component.html', + styleUrl: './uglify.component.scss' +}) +export class UglifyComponent { + input: string = JSON.stringify({first_name: "John", last_name: "Doe"}, null, 2); + inputOptions = {theme: 'vs-dark', language: 'json', readOnly: false}; + output: string = JSON.stringify({first_name: "John", last_name: "Doe"}); + outputOptions = {theme: 'vs-dark', language: 'json', readOnly: true}; + + constructor(private service: JsonTransformService) { + } + + handleInputChange($event: any) { + console.log($event); + this.service + .uglify($event) + .subscribe({ + next: response => { + console.log(response); + this.output = response.body.result; + }, + error: response => { + console.log(response) + if (response.status === 499) { + this.output = response.error.detail; + console.log(response.error.detail); + } + } + }); + } +} diff --git a/DevDisciples.Json.Tools.CLI/Json2CSharpCommand.CommandOptions.cs b/DevDisciples.Json.Tools.CLI/Json2CSharpCommand.CommandOptions.cs index 57ee812..789b259 100644 --- a/DevDisciples.Json.Tools.CLI/Json2CSharpCommand.CommandOptions.cs +++ b/DevDisciples.Json.Tools.CLI/Json2CSharpCommand.CommandOptions.cs @@ -4,7 +4,7 @@ public partial class Json2CSharpCommand { public class CommandOptions : CLI.CommandOptions { - public string? RootClassName { get; set; } - public string? Namespace { get; set; } + public string? RootClassName { get; init; } + public string? Namespace { get; init; } } } \ No newline at end of file diff --git a/DevDisciples.Json.Tools.CLI/JsonPrettifyCommand.CommandOptions.cs b/DevDisciples.Json.Tools.CLI/JsonPrettifyCommand.CommandOptions.cs index 96f522a..48cd598 100644 --- a/DevDisciples.Json.Tools.CLI/JsonPrettifyCommand.CommandOptions.cs +++ b/DevDisciples.Json.Tools.CLI/JsonPrettifyCommand.CommandOptions.cs @@ -4,6 +4,6 @@ public partial class JsonPrettifyCommand { public class CommandOptions : CLI.CommandOptions { - public int IndentSize { get; set; } + public int IndentSize { get; init; } } } \ No newline at end of file diff --git a/DevDisciples.Json.Tools/Json2CSharpTranslator.ClassTranslation.cs b/DevDisciples.Json.Tools/Json2CSharpTranslator.ClassTranslation.cs index 43a5abe..ea830a8 100644 --- a/DevDisciples.Json.Tools/Json2CSharpTranslator.ClassTranslation.cs +++ b/DevDisciples.Json.Tools/Json2CSharpTranslator.ClassTranslation.cs @@ -4,10 +4,10 @@ namespace DevDisciples.Json.Tools; public static partial class Json2CSharpTranslator { - public struct ClassTranslation : ITranslation + public readonly struct ClassTranslation : ITranslation { - public string Name { get; set; } - public List Properties { get; set; } + public string Name { get; init; } + public List Properties { get; init; } public void Translate(Context context) { diff --git a/DevDisciples.Json.Tools/Json2CSharpTranslator.Context.cs b/DevDisciples.Json.Tools/Json2CSharpTranslator.Context.cs index f6090a4..7e7848d 100644 --- a/DevDisciples.Json.Tools/Json2CSharpTranslator.Context.cs +++ b/DevDisciples.Json.Tools/Json2CSharpTranslator.Context.cs @@ -9,9 +9,9 @@ public static partial class Json2CSharpTranslator public const string DefaultRootClassName = "Root"; public const string DefaultNamespace = "My.Namespace"; - public string RootClassName { get; set; } = DefaultRootClassName; - public string Namespace { get; set; } = DefaultNamespace; - public List Classes { get; set; } = new(); + public string RootClassName { get; init; } = DefaultRootClassName; + public string Namespace { get; init; } = DefaultNamespace; + public List Classes { get; } = new(); public readonly StringBuilder Builder = new(); } } \ No newline at end of file diff --git a/DevDisciples.Json.Tools/Json2CSharpTranslator.JsonArrayTranslator.cs b/DevDisciples.Json.Tools/Json2CSharpTranslator.JsonArrayTranslator.cs index 6411c91..604fb90 100644 --- a/DevDisciples.Json.Tools/Json2CSharpTranslator.JsonArrayTranslator.cs +++ b/DevDisciples.Json.Tools/Json2CSharpTranslator.JsonArrayTranslator.cs @@ -40,7 +40,7 @@ public static partial class Json2CSharpTranslator }; } - private static ClassTranslation SquashObjects(string className, List objects, object[] args) + private static ClassTranslation SquashObjects(string className, IEnumerable objects, object[] args) { var classes = objects .Select(@object => JsonObjectTranslator.Translate(@object, args)) diff --git a/DevDisciples.Json.Tools/Json2CSharpTranslator.PropertyTranslation.cs b/DevDisciples.Json.Tools/Json2CSharpTranslator.PropertyTranslation.cs index f206624..989d80b 100644 --- a/DevDisciples.Json.Tools/Json2CSharpTranslator.PropertyTranslation.cs +++ b/DevDisciples.Json.Tools/Json2CSharpTranslator.PropertyTranslation.cs @@ -4,10 +4,10 @@ namespace DevDisciples.Json.Tools; public static partial class Json2CSharpTranslator { - public struct PropertyTranslation : ITranslation + public readonly struct PropertyTranslation : ITranslation { - public string Name { get; set; } - public string Type { get; set; } + public string Name { get; init; } + public string Type { get; init; } public void Translate(Context context) { diff --git a/DevDisciples.Json.Tools/JsonFormatter.Context.cs b/DevDisciples.Json.Tools/JsonFormatter.Context.cs index f76c939..9090e48 100644 --- a/DevDisciples.Json.Tools/JsonFormatter.Context.cs +++ b/DevDisciples.Json.Tools/JsonFormatter.Context.cs @@ -9,10 +9,10 @@ public static partial class JsonFormatter public const int DefaultIndentSize = 2; protected int Depth { get; set; } = 0; - public StringBuilder Builder { get; set; } = new(); - public bool Beautify { get; set; } = false; + public StringBuilder Builder { get; } = new(); + public bool Beautify { get; init; } = false; public string Indent => new(' ', Depth); - public int IndentSize { get; set; } = DefaultIndentSize; + public int IndentSize { get; init; } = DefaultIndentSize; public string NewLine => Beautify ? "\n" : ""; public string Space => Beautify ? " " : ""; diff --git a/DevDisciples.Json.Tools/JsonFormatter.cs b/DevDisciples.Json.Tools/JsonFormatter.cs index 4871a9e..786efd8 100644 --- a/DevDisciples.Json.Tools/JsonFormatter.cs +++ b/DevDisciples.Json.Tools/JsonFormatter.cs @@ -44,12 +44,12 @@ public static partial class JsonFormatter context.Builder.Append($"[{context.NewLine}"); context.IncrementDepth(); - for (var i = 0; i < array.Elements.Count; i++) + for (var i = 0; i < array.Elements.Length; i++) { var node = array.Elements[i]; context.Builder.Append(context.Indent); Visitors[node.GetType()](node, args); - if (i < array.Elements.Count - 1) context.Builder.Append($",{context.NewLine}"); + if (i < array.Elements.Length - 1) context.Builder.Append($",{context.NewLine}"); } context.DecrementDepth(); @@ -64,12 +64,12 @@ public static partial class JsonFormatter context.Builder.Append($"{{{context.NewLine}"); context.IncrementDepth(); - var count = @object.Properties.Count; + var count = @object.Properties.Length; for (var i = 0; i < count; i++) { - var (key, node) = @object.Properties.ElementAt(i); - context.Builder.Append($"{context.Indent}\"{key}\":{context.Space}"); - Visitors[node.GetType()](node, args); + var property = @object.Properties.ElementAt(i); + context.Builder.Append($"{context.Indent}\"{property.Key}\":{context.Space}"); + Visitors[property.Value.GetType()](property.Value, args); if (i < count - 1) context.Builder.Append($",{context.NewLine}"); } diff --git a/DevDisciples.Parsing/Lexer.Token.cs b/DevDisciples.Parsing/Lexer.Token.cs index 4955bb2..83f2f46 100644 --- a/DevDisciples.Parsing/Lexer.Token.cs +++ b/DevDisciples.Parsing/Lexer.Token.cs @@ -2,7 +2,7 @@ public abstract partial class Lexer where TToken : Enum { - public struct Token : ISourceLocation + public readonly struct Token : ISourceLocation { public string File { get; } public TToken Type { get; } diff --git a/DevDisciples.Parsing/VisitorContainer.cs b/DevDisciples.Parsing/VisitorContainer.cs index f9f9eb0..3adc725 100644 --- a/DevDisciples.Parsing/VisitorContainer.cs +++ b/DevDisciples.Parsing/VisitorContainer.cs @@ -16,7 +16,7 @@ public class VisitorContainer public class VisitorContainer { protected Dictionary> Visitors { get; } = new(); - public Visitor.Visit Default { get; set; } = default!; + public Visitor.Visit Default { get; } = default!; public VisitorContainer Register(Visitor.Visit visitor) @@ -31,7 +31,7 @@ public class VisitorContainer public class VisitorContainer { protected Dictionary> Visitors { get; } = new(); - public Visitor.Visit Default { get; set; } = default!; + public Visitor.Visit Default { get; } = default!; public void Register(Visitor.Visit visitor) {