Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions projects/demo/src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ <h3>Primatives</h3>
<ngx-json-treeview [json]="'hello, world!'" />
<ngx-json-treeview [json]="true" />
<ngx-json-treeview [json]="null" />
<ngx-json-treeview [json]="{}" />
</div>

<h3>Arrays</h3>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@if (segments().length === 0) {
<section [class]="'segment-type-' + rootType()">
<span class="segment-primative">
{{ json() ?? 'null' }}
{{ asString() }}
</span>
</section>
} @else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ export class NgxJsonTreeviewComponent {
return ']';
} else return '}';
});
asString = computed<string>(() => JSON.stringify(this.json(), null, 2));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The new asString computed property correctly uses JSON.stringify to provide a string representation for root values when no segments are generated (e.g., for primitives, empty objects, or empty arrays). This fixes the issue where empty objects might have rendered as [object Object].

The use of JSON.stringify(this.json(), null, 2) introduces newlines and indentation (e.g., {\n \n} for an empty object). Currently, HTML's default whitespace collapsing behavior renders this as { }, which is a good outcome.

However, for improved robustness against potential future CSS changes (e.g., if .segment-primative or its container ever gets white-space: pre or white-space: pre-wrap), this could lead to multi-line displays for these empty structures, which might not be ideal for this compact "primitive" view.

Have you considered using JSON.stringify(this.json()) (without the null, 2 arguments) for this specific asString computed property? This would produce compact strings like "{}" and "[]" directly, ensuring a single-line representation irrespective of white-space styling, while still correctly rendering other primitives like strings, numbers, and booleans in their JSON string form.

Suggested change
asString = computed<string>(() => JSON.stringify(this.json(), null, 2));
asString = computed<string>(() => JSON.stringify(this.json()));


isExpandable(segment: Segment) {
return (
Expand Down