New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

angular2-prettyjson

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

angular2-prettyjson

Angular2 json utils. Includes a pipe to replace Angular's built in json pipe which implements spacing, avoids circular references. Also includes a component that will pretty print json with syntax highlight

  • 2.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.2K
decreased by-35.26%
Maintainers
1
Weekly downloads
 
Created
Source

Angular 2 Pretty Json v2.0.1

A module for Angular 2 debug output of objects. Contains a pipe similar to JsonPipe but adds support for spacing and handling of circular structures.
Also contains a component that outputs any object with syntax highlight.
Warning: just as the JsonPipe, this is an impure pipe and should be used only for debugging purposes.

Install

npm install angular2-prettyjson

Usage

Import PrettyJsonModule to have access to following component and pipes

import {PrettyJsonModule} from 'angular2-prettyjson';

@NgModule({
    declarations: [
        AppComponent,
    ],
    imports: [
        PrettyJsonModule,
    ],
    providers: [
    ],
    bootstrap: [AppComponent]
})
export class AppModule {
}

Safe Pipe

The SafeJsonPipe aims to override the JsonPipe and so uses the same name "json". It also accepts an optional argument spaces=2 for the JSON stringify spacing.

@Component({
  ....
  template: `
    <pre>
    {{ circularObj | json }}
    {{ circularObj | json:4 }}
    </pre>
  ` // make sure to use a surrounding element with white-space: pre; for best results
  })
  ...

outputs

2 spaces (default):

2 spaces

4 spaces:

4 spaces

Overriding JsonPipe throughout the app

If you want the Safe Pipe to be used throughout the app:

import {PrettyJsonModule, SafeJsonPipe} from 'angular2-prettyjson';
import {JsonPipe} from '@angular/common';

@NgModule({
    declarations: [
        AppComponent,
    ],
    imports: [
        PrettyJsonModule,
    ],
    providers: [
            { provide: JsonPipe, useClass: SafeJsonPipe }
    ],
    bootstrap: [AppComponent]
})
export class AppModule {
}

Pretty (and safe) Pipe

The PrettyJsonPipe stringifies the object and then adds spans around properties, null, arrays etc. You can bind it to the innerHtml of other elements.


@Component({
  ....
  template: `
    <pre [innerHtml]="circularObj | prettyjson:3"></pre>
  `
  })
  ...

A good set of styles to use is

   .string { color: green; }
   .number { color: darkorange; }
   .boolean { color: blue; }
   .null { color: magenta; }
   .key { color: red; }

See output under component below.

Component

Creates a pre element into which the Pretty Json pipe'd object is dumped as HTML. Takes care of styling.

Takes an input [obj] that can be data bound to any object.

import {PrettyJsonComponent} from 'angular2-prettyjson';

@Component({
  ....
  entryComponents: [PrettyJsonComponent], // Add to entry components
  template: `
    <prettyjson [obj]="theForm.value"></prettyjson>
  `
  })
  export class MyComponent {
    ngOnInit() {
      this.theForm = this.formBuilder.group({
       ...

outputs

Pretty json with syntax highlight

Changelog

  1. Export module, both pipes and the component at the top level of the module
  2. Update README

Keywords

FAQs

Package last updated on 26 Nov 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc