Skip to content
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

fix: process null values #1248

Merged
merged 17 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 3 additions & 3 deletions packages/abstractions/src/serialization/parseNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface ParseNode {
* Gets the string value of the node.
* @return the string value of the node.
*/
getStringValue(): string | undefined;
getStringValue(): string | null | undefined;
baywet marked this conversation as resolved.
Show resolved Hide resolved
/**
* Gets a new parse node for the given identifier.
* @param identifier the identifier of the current node property.
Expand All @@ -31,12 +31,12 @@ export interface ParseNode {
* Gets the boolean value of the node.
* @return the boolean value of the node.
*/
getBooleanValue(): boolean | undefined;
getBooleanValue(): boolean | null | undefined;
/**
* Gets the Number value of the node.
* @return the Number value of the node.
*/
getNumberValue(): number | undefined;
getNumberValue(): number | null | undefined;
baywet marked this conversation as resolved.
Show resolved Hide resolved
/**
* Gets the Guid value of the node.
* @return the Guid value of the node.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ export interface SerializationWriter {
* @param key the key to write the value with.
* @param value the value to write to the stream.
*/
writeStringValue(key?: string | undefined, value?: string | undefined): void;
writeStringValue(key?: string | undefined, value?: string | null | undefined): void;
baywet marked this conversation as resolved.
Show resolved Hide resolved
/**
* Writes the specified boolean value to the stream with an optional given key.
* @param key the key to write the value with.
* @param value the value to write to the stream.
*/
writeBooleanValue(key?: string | undefined, value?: boolean | undefined): void;
writeBooleanValue(key?: string | undefined, value?: boolean | null | undefined): void;
/**
* Writes the specified number value to the stream with an optional given key.
* @param key the key to write the value with.
* @param value the value to write to the stream.
*/
writeNumberValue(key?: string | undefined, value?: number | undefined): void;
writeNumberValue(key?: string | undefined, value?: number | null | undefined): void;
/**
* Writes the specified Guid value to the stream with an optional given key.
* @param key the key to write the value with.
Expand Down
6 changes: 3 additions & 3 deletions packages/serialization/json/src/jsonParseNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ export class JsonParseNode implements ParseNode {
constructor(private readonly _jsonNode: unknown) {}
public onBeforeAssignFieldValues: ((value: Parsable) => void) | undefined;
public onAfterAssignFieldValues: ((value: Parsable) => void) | undefined;
baywet marked this conversation as resolved.
Show resolved Hide resolved
public getStringValue = () => (typeof this._jsonNode === "string" ? (this._jsonNode as string) : undefined);
public getStringValue = () => (typeof this._jsonNode === "string" || this._jsonNode == null ? this._jsonNode : undefined);
baywet marked this conversation as resolved.
Show resolved Hide resolved
public getChildNode = (identifier: string): ParseNode | undefined => (this._jsonNode && typeof this._jsonNode === "object" && (this._jsonNode as { [key: string]: any })[identifier] !== undefined ? new JsonParseNode((this._jsonNode as { [key: string]: any })[identifier]) : undefined);
public getBooleanValue = () => (typeof this._jsonNode === "boolean" ? (this._jsonNode as boolean) : undefined);
public getNumberValue = () => (typeof this._jsonNode === "number" ? (this._jsonNode as number) : undefined);
public getBooleanValue = () => (typeof this._jsonNode === "boolean" || this._jsonNode == null ? this._jsonNode : undefined);
baywet marked this conversation as resolved.
Show resolved Hide resolved
public getNumberValue = () => (typeof this._jsonNode === "number" || this._jsonNode == null ? this._jsonNode : undefined);
public getGuidValue = () => parseGuidString(this.getStringValue());
public getDateValue = () => (this._jsonNode ? new Date(this._jsonNode as string) : undefined);
public getDateOnlyValue = () => DateOnly.parse(this.getStringValue());
Expand Down
50 changes: 37 additions & 13 deletions packages/serialization/json/src/jsonSerializationWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,53 @@ export class JsonSerializationWriter implements SerializationWriter {
public onBeforeObjectSerialization: ((value: Parsable) => void) | undefined;
public onAfterObjectSerialization: ((value: Parsable) => void) | undefined;
public onStartObjectSerialization: ((value: Parsable, writer: SerializationWriter) => void) | undefined;
public writeStringValue = (key?: string, value?: string): void => {
if (value !== null && value !== undefined) {
key && this.writePropertyName(key);
this.writer.push(JSON.stringify(value));
key && this.writer.push(JsonSerializationWriter.propertySeparator);
public writeStringValue = (key?: string, value?: string | null): void => {
if (value === undefined) {
return;
}

if (value === null) {
key && this.writeNullValue(key);

return;
}

key && this.writePropertyName(key);
this.writer.push(JSON.stringify(value));
key && this.writer.push(JsonSerializationWriter.propertySeparator);
};
private writePropertyName = (key: string): void => {
this.writer.push(`"${key}":`);
};
public writeBooleanValue = (key?: string, value?: boolean): void => {
if (value !== null && value !== undefined) {
key && this.writePropertyName(key);
this.writer.push(`${value}`);
key && this.writer.push(JsonSerializationWriter.propertySeparator);
if (value === undefined) {
return;
}

if (value === null) {
key && this.writeNullValue(key);

return;
}

key && this.writePropertyName(key);
this.writer.push(`${value}`);
key && this.writer.push(JsonSerializationWriter.propertySeparator);
};
public writeNumberValue = (key?: string, value?: number): void => {
if (value !== null && value !== undefined) {
key && this.writePropertyName(key);
this.writer.push(`${value}`);
key && this.writer.push(JsonSerializationWriter.propertySeparator);
if (value === undefined) {
return;
}

if (value === null) {
key && this.writeNullValue(key);

return;
}

key && this.writePropertyName(key);
this.writer.push(`${value}`);
key && this.writer.push(JsonSerializationWriter.propertySeparator);
};
public writeGuidValue = (key?: string, value?: Guid): void => {
if (value) {
Expand Down
Loading