Posts

Parse JSON string in Typescript

Typescript is typed superset of Javascript. It has extension of .ts, it finally gets compiled to plain Java Script. Here we will see how we can parse a string representing JSON data and map it to TypeScript Object. Say the following is our UserDetails object export class UserDetails { name : string ; firstName : string ; lastName : string ; sessionToken : string ; roles : Array < string >; constructor ( name ?: string , firstName ?: string , lastName ?: string , sessionToken ?: string ) { this . name = name ; this . firstName = firstName ; this . lastName = lastName ; this . sessionToken = sessionToken ; } And the JSON string as follows, { "name" : "Sidd" , "firstName" : "Sidd" , "lastName" : "Bhatt" , "sessionToken" : "d501dce2-a628-4e4c-9ea0-1b2ea31f5270" , "rememberMe" : false , "authentic...

Angular Switch Statement

The ngSwitch directive is an inbuilt directive that lets you hide/show HTML elements depending on an expression. NgSwitch uses ngSwitchCase keyword to define a set of possible element which will be selected based on the ngSwitchCase <element ngSwitch="expression">   <element *ngSwitchCase="value"></element>   <element *ngSwitchCase="value"></element>   <element *ngSwitchCase="value"></element> </element> For Example say we want to design a simple Component which renders different kind of Input fields like Text, TextArea etc based on the parameters we are sending. Then we need some kind of If/Else or Switch statements to select the case. Following Plunker demonstrates working example of a ngWitchCase statement. Here is the link to the Plunker to try it out. https://plnkr.co/edit/AliFkb?p=catalogue

Sanitize URL in Angular 4 - Working with Base64 Images

Image
Handling Images is a very common in most of the web applications. Typically we need to render image in a webpage (very common) and some times we need to provide an user interface to upload images, editing images etc. We can save the uploaded image in the web application in multiple ways. For instance we can save images in base64 encoded format in database or we can save the uploaded images in file system on the server and store the image path in database. It has it's own advantages and disadvantages. Angular applications sometimes does not show the image if the image is in base64 econdoed form. While loading base64 images in Angular Application, based on different factors like Content Security Policy etc, Angular does not allow to load base64 encoded images in the HTML. It will append unsafe to the url, as a result the HTML does not show the image properly. For instance if I want to show the following image which is in base64. data:image/png;base64,aaaiskkAAAANSUhEUgAAAPA...

Rest API Design Reference

REST stands for Representational State Transfer. It is an architectural style for developing software. Architectural Constraints REST defines 6 architectural constraints which make any web service – a true RESTful API. Uniform interface Client–server Stateless Cacheable Layered system Code on demand Uniform interface Uniform interface suggests that the resource in a system to have only one logical URI and  Then provide additional information to retrieve the related data. For instance we can provide Links for related resources like child objects, parent objects etc. User of one API end point should be able to identify and work similarly with other Resources. Also, the resource representations across system should follow certain guidelines such as naming conventions, link formats or data format, filter formats pagination formats etc. All resources should be accessible in similar way and should use similar status codes. Client–server Server should not depend on p...

Java Constructor vs method

Functionally difference between constructors and methods is that constructors create and initialize objects that don't exist yet, while methods perform operations on objects that already exist. Constructors can't be called directly. They are invoked implicitly when the new keyword creates an object. Methods can be called directly on an object that has already been created with new. Constructors must be named with the same name as the class name. Constructors cannot  return anything, even void . Methods must be declared to return something ( or void). Constructor compiler provide default constructor where as method compiler does't provide. Constructor cannot be final since it cant be inherited. Constructor cannot be abstract since it is used to create and initialize object. Constructor cannot be static since it is used to create instance of object. Only public, protected & private modifiers are permitted.

immutable objects in java

In software programming, an immutable object is an object whose state cannot be modified after it is created. How to make Immutable Object in Java Make the class final. Make all members final, set them explicitly, in a static block, or in the constructor. Make all members private. Do not expose any setter methods. If the instance fields include references to mutable objects, then don't share references to the mutable objects.  For getter methods which needs to return mutable objects, create copies of your internal mutable objects when necessary to avoid returning the originals in your methods. Do not store references to external mutable objects passed to the constructor. Create copies, and store references to the copies.  Advantage of Immutable Objects Immutable Objects are ThreadSafe. Value Objects are generally considered Immutable. In java String and all the Wrapper Classes like Integer, Boolean are immutable. It enhances security and less bug when the o...

Better ways to implement Singleton in Java

Singleton Design pattern restrict creation of only once instance of a Class. That means inside a single JVM only one instance of the Singleton Class will be created.  In java we have broadly two options to implement Singleton. 1) Early Creation of the Object Instance. 2) Lazy Creation of the Object Instance: Here the instance is created on first invocation. Following example demonstrate how to implement Singleton in Java. Method 1: Early Creation of Object Instance public class SingletonClass { private static final SingletonClass INSTANCE = new SingletonClass(); //Private Constructor private SingletonClass () { //Do nothing } public static SingletonClass getInstance () { return INSTANCE ; } } Here the instance is created ahead even before calling the Class. We need to provide a private constructor otherwise Java Compiler will add a default Constructor. Method 2: Lazy Creation of Object Inst...