Add JSON serialization support for CadObject instances#1076
Open
DomCR wants to merge 32 commits into
Open
Conversation
Updated `ACadSharp.csproj` to use wildcard package versions for better compatibility. Introduced `JsonConverter`, `CadConverterFactory`, `CadDocumentConverter`, and `CommonCadConverter` classes to handle serialization and deserialization of `CadObject` and `CadDocument` types. Added support for both `System.Text.Json` and `Newtonsoft.Json` using conditional compilation for framework compatibility.
This reverts commit faf527e.
- Added assertions for `CadObject.ObjectName` and `CadObject.SubclassMarker` in `JsonConverterTests.cs`. - Introduced `_converters` dictionary in `CadConverterFactory.cs` for type-specific converters. - Updated `CanConvert` and `CreateConverter` methods in `CadConverterFactory.cs` to utilize `_converters`. - Added `_idProperties` in `CommonCadConverter.cs` to handle specific properties during serialization. - Updated `Write` method in `CommonCadConverter.cs` to explicitly write `ObjectName` and `SubclassMarker`. - Updated `CSUtilities` submodule to a new commit. - Added `ColorConverter` class for JSON serialization of `Color` objects.
Enhanced JSON serialization with new converters and tests: - Added `CadDocumentToJsonTest` for `CadDocument` serialization. - Implemented `Deserialize` and refactored `Serialize` in `CadJsonConverter`. - Added `CadHeaderConverter` for custom `CadHeader` serialization. - Updated `CadDocumentConverter` and `ColorConverter` for JSON writing. - Refactored `CommonCadConverter` to exclude specific properties. - Adjusted `DimensionStyle` property initialization.
Introduced conditional compilation to support both `System.Text.Json` and `Newtonsoft.Json` serialization libraries. Refactored tests and core classes (`JsonConverterTests`, `CadConverterFactory`, and `CommonCadConverter`) to ensure compatibility. Added `JsonWrapper` utility class to abstract serialization differences. Updated `CSUtilities` submodule reference.
Refactored `CadDocumentConverter` and `CommonCadConverter` to use conditional compilation for .NET and other frameworks. Updated serialization methods to ensure compatibility by using `JsonSerializer.Serialize` for .NET and `serializer.Serialize` for other frameworks. Adjusted method signatures and added `NotImplementedException` for unimplemented methods.
- Added `JsonToEntityTest` in `JsonConverterTests.cs` to test JSON serialization/deserialization of `Entity` objects. - Introduced `Deserialize` method in `CadJsonConverter.cs` for type-based deserialization of `CadObject` instances. - Implemented platform-specific `deserialize` method in `CadJsonConverter.cs` for JSON options handling. - Updated `CadHeaderConverter` and `ColorConverter` to implement `Read` methods for JSON deserialization. - Enhanced `CommonCadConverter` to handle unwritable properties, null values, and `INamedCadObject` deserialization. - Added missing `using` directives in `CommonCadConverter.cs`.
Updated `EntityFactory` to support additional entity types (`Polyline3D`, `PolyfaceMesh`, `PolygonMesh`) and refactored randomization methods. Simplified `RandomizePolyline<T>` with generic constraints. Introduced `writeEnumerable` in `CommonCadConverter` for enumerable serialization, supporting both `System.Text.Json` and `Newtonsoft.Json`. Moved `CommonPolylineConverter<T>` to a new file with updated logic. Removed `CSUtilities.Extensions` references and cleaned up conditional compilation. Performed general code cleanup and reorganization for better maintainability.
Extracted `readPropertyValue` in `CommonCadConverter<T>` to improve reusability and allow overrides. Updated `CommonPolylineConverter<T>` to handle `Vertices` deserialization with a custom implementation while delegating other properties to the base class.
Refactored `CommonPolylineConverter` to use two generic parameters (`TPolyline` and `TVertex`) for better type safety and updated `CadConverterFactory` to use the new implementation. Added a new `PolyfaceMeshConverter` for handling `PolyfaceMesh` objects, including serialization of `Faces`. Reorganized `AecBinRecord` class by moving `BinaryData` property, adding constructors, and removing redundant `Name` property to streamline initialization and improve clarity.
Refactored `CommonPolylineConverter` to remove `PolyfaceMeshConverter` and added its implementation in a new file with JSON deserialization support. Updated `ImageDefinition` and `VisualStyle` classes to include additional properties, XML documentation, and DXF serialization attributes. Improved code structure, readability, and maintainability.
Introduced a generic `readArray<T>` method in `CommonPolylineConverter.cs` to handle array deserialization in a reusable way. Updated `CommonPolylineConverter` and `PolyfaceMeshConverter` to use this method, improving code reuse and readability. Removed redundant inline deserialization code.
Added static DeserializeDocument to CadJsonConverter (not yet implemented). Updated tests to use this new method. Changed Deserialize<T> to use internal deserialization logic instead of throwing NotImplementedException.
Add JSON deserialization support for CadDocument and CadHeader, including improved handling of DateTime and TimeSpan in CadHeader. Implement CadDocumentConverter.Read for .NET, add overloads and documentation to CadJsonConverter, and introduce JsonDocumentBuilder for deserialization support. Also, fix INamedCadObject Owner serialization in CommonCadConverter.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Add json converters to allow the serialization of the CadObjects and documents into json format.
Tasks done in this PR
CadObjectSerialization.CadObjectDeserialization.CadDocumentSerialization.CadDocumentDeserialization.TODO
Consider move the non CAD formats to a separate library:
ACadSharp.Formats