Skip to content

Add JSON serialization support for CadObject instances#1076

Open
DomCR wants to merge 32 commits into
masterfrom
format-json
Open

Add JSON serialization support for CadObject instances#1076
DomCR wants to merge 32 commits into
masterfrom
format-json

Conversation

@DomCR

@DomCR DomCR commented May 17, 2026

Copy link
Copy Markdown
Owner

Description

Add json converters to allow the serialization of the CadObjects and documents into json format.

Tasks done in this PR

  • Json CadObject Serialization.
  • Json CadObject Deserialization.
  • Json CadDocument Serialization.
  • Json CadDocument Deserialization.

TODO

Consider move the non CAD formats to a separate library: ACadSharp.Formats

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.
@DomCR DomCR added the feature New feature added label May 17, 2026
DomCR added 28 commits May 17, 2026 17:55
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.
DomCR added 3 commits June 23, 2026 09:11
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature added

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant