Skip to content

[WIP] feat: JSON Deserializer/Denormalizer #506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ written in _TypeScript_ and compiled for the target.
* Support the downstream implementation of custom XML-serializers tailored to specific environments
by providing an abstract base class that takes care of normalization and BomRef-discrimination.
This is done, because there is no universal XML support in _JavaScript_.
* Deserialization:
* Provide a JSON-deserializer for all target environments.

## Capabilities

Expand Down Expand Up @@ -82,8 +84,10 @@ written in _TypeScript_ and compiled for the target.
* `1.3`
* `1.2`
* Normalizers that convert data models to JSON structures
* Denormalizers that convert JSON structures to data models
* Normalizers that convert data models to XML structures
* Universal serializer that converts `Bom` data models to JSON string
* Deserializer that converts JSON string to `Bom` data models
* Serializer that converts `Bom` data models to XML string:
* Specific to _WebBrowsers_: implementation utilizes browser-specific document generators and printers.
* Specific to _Node.js_: implementation plugs/requires/utilizes one of the following _optional_ libraries
Expand Down
44 changes: 44 additions & 0 deletions src/serialize/baseDeserializer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*!
This file is part of CycloneDX JavaScript Library.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

SPDX-License-Identifier: Apache-2.0
Copyright (c) OWASP Foundation. All Rights Reserved.
*/

import type { Bom } from '../models'
import type { DenormalizerOptions, Deserializer, DesserializerOptions } from './types'

export abstract class BaseDeserializer<NormalizedBom> implements Deserializer {
/**
* @readonly
* @throws {@link Error}
*/
public deserialize (str: string, options?: DesserializerOptions & DenormalizerOptions): Bom {
return this._denormalize(
this._deserialize(str, options),
options
)
}

/**
* @throws {@link Error}
*/
protected abstract _denormalize (bom: any, options?: DenormalizerOptions): Bom

/**
* @throws {@link Error}
*/
protected abstract _deserialize (data: string, options?: DesserializerOptions): any
}
2 changes: 1 addition & 1 deletion src/serialize/index.common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ export * from './baseSerializer'
// region JSON

export * as JSON from './json'
export * from './jsonDeserializer'
export * from './jsonSerializer'
// export * from './jsonDeserializer' // @TODO

// endregion JSON

Expand Down
Loading