Showing posts with label JSON. Show all posts
Showing posts with label JSON. Show all posts

Friday, November 14, 2014

"JavaScript: The Good Parts" - book review

In this post I'll explain my thoughts on "JavaScript: The Good Parts" written by Douglas Crockford. It is fairly old book written in 2008 but still does the good job of teaching you what to use and what not to use from JavaScript. As the author says, this book has lot of information packed in about 150 pages, which means you have to read at least twice each chapter. Yes I passed each chapter twice and also spent some time writing the code from the book in Firefox/Firebug just to see how it goes. I think more examples to practice would be much better for the book as it lacks them. But in the end you would certainly need to read another book to learn how to code proper JavaScript modules, patterns, etc... I am planning to read another book because this one is not enough to get you going with JS. My recomendation for next book would be this one: "JavaScript Patterns" - by Stoyan Stefanov, it has about 240 pages which is enough.

The book is structured in 10 chapters and with 5 appendices, A-E. My suggestion is to read all chapters includding the appendices. Appendix D is just the railway diagrams, so you may skip it.
Chapters:
  1. Good Parts. This chapter contains basic info about JS and how to set up testing ground for other code examples in the book. Don't skip it. 
  2. Grammar. Contains basic information about reserved keywords in JS, data types, expressions, functions etc...
  3. Objects. Explain how to work with Objects in JavaScript, how to create new object, how to retrieve infrormation from object, object prototype, reflection, enumeration, delete operator, etc...
  4. Functions. Dives into JavaScript function basics. How to create new function, how to invoke a function, this and arguments objects of a function, its connection with objects. Each function is an object so you can add a function to its prototype. Function scope, closure, function modules, cascade (builder pattern), curry and memoization.
  5. Ineritance. Inheritance types, implementing standard OOP in JS (pseudoclassical) inheritance, prototypal and functional inheritance. Very important chapter, don't skip it.
  6. Arrays. Array basics, length property, useful array methods, delete operator, enumeration, etc...
  7. Regular Expressions. Dives into JS regular expressions, what are they and how to use them. Couple of basic rules how do RE work. Very useful thing, especially when processing strings. 
  8. Methods. This chapter explains implemented methods that come with JavaScript itself. You may want to skip this chapter but it would be helpful if you read it. 
  9. Style. This chapter explains some techniques how to write error prone JavaScrtipt code. My suggestion is to not skip this chapter. 
  10. Beautiful features. Read this chapter to learn what are the best parts of JavaScript that you should use. Functions as a first class object, Dynamic objects with prototypal inheritance and Object litrals and array literals.
  11. Appendix A. Awful Parts. Explains all of the worse parts in JS and how to avoid them.Global variables, scope, semicolon insertion, reserved words, unicode, typeof operator, parseInt function, + operator, floating point, NaN, phony arrays, falsy values, hasOwnProperty function and Object.
  12. Appendix B. Bad parts. Things you can live with but still should avoid. == vs === operators, with statement, eval function, continue statement, switch fall trough, block-less statements, ++ and -- operators vs += and -= operators, bitwise operators, function statement vs function expression, typed wrappers, new operator, void keyword.
  13. Appendix C. JSLint. An online tool build by Douglas himself. Use it whenever possible to debug your JS code. You may find this tool also useful, http://jsbeautifier.org/
  14. Appendix D. Syntax Diagrams. If you are beginner at programming, these railway diagrams will help you get going. 
  15. Appendix E. JSON. Explains what is JSON, JSON syntax, how to use it securely and a complete script of JSON parser in JavaScript.
 I think this book is not recommended for beginner programmers, but it is good if you are learning JavaScript. Still it lacks more code examples and practices, so you have to read another JS book. My opinion of this book is 4.5/5. It does the job for what is meant to do, aka teaching you pros and cons of JavaScript and how to use or avoid them. You must read this book if plan to code JS programs in future.

Saturday, July 16, 2011

Installing MongoDB

I use MongoDB on Ubuntu but you can also use it on Windows. I will explain how to install it on Ubuntu.

I installed myselft with oppening terminal and typing:

$ sudo apt-get install mongodb

Wait a moment and it will be installed. There is an option to build it from binary sources but this is much more complex (In other post).

Next, you must create directories for the db so open terminal and type in these commands:

$ sudo mkdir -p /data/db/
$ sudo chown `id -u` /data/db 
 
This creates folder data/db at root level and change owner to root. 
Now you should be able to use MongoDB. Start the server by typing in terminal:
 
$ sudo start mongodb  or this $ service mongodb start. You will get a message 
whether the service is started. If it is started than go to this folder /bin and type  
mongo in terminal i.e
 
$ cd /bin
$ mongo    you will get the ">" char in terminal so it means it works.
For start type in 
 
> use mydb 
> db.testcol.save({json : "this is a json object"});
> db.testcol.find();
You should get something like this as output:
{ "_id" : ObjectId("4c2209f9f3924d31102bd84a"), "json" : "this is a json object" }
 
With these commands we use the database called mydb and than create a collection 
(table) called testcol and save one BSON document (row)  
{json : "this is a json object"}. The find command prints all document in the collection  
testcol. You should visit the SQL to Mongo Mapping page at SQL-Mongo
 
You can notice that we use json notation to create documents (rows) in the database, 
Mongo works its way i.e it constantly converts from json to bson and vice verse. 
 
I will add some examples on how to work with Mongo so make sure you read well 
about json.  
 
 You might wanna visit the downloads page @ downloads
Select you operating system version and download it.  Also visit Quickstart

Thursday, July 14, 2011

Example for converting xml document to JSON

We have this example of xml

<menu id="file" value="File" number="50" >
     <popup>
        <menuitem value="New" onclick="CreateNewDoc()" />
        <menuitem value="Open" onclick="OpenDoc()" />
       <menuitem value="Close" onclick="CloseDoc()" />
     </popup>
</menu>

So I will shortly explain the alghorithm to convert xml to 
json code. 
Every json document starts and ends with curly 
brackets {}. Next, menu is a top level tag and contains several 
other subtags so menu is an object. Atributes in the menu tag 
are name/value pairs so in the first step we have this:
 
{
  "menu" : {
  "id" : "file",
  "value" : "File",
  "number" : 50,
  "popup" : ...
}
}

Note that numbers aren't quoted only strings are quoted.
 
Next we will develop the popup and menuitem tags. popup has 
several tags in it so it is an object. Also there are three 
menuitem tags so we need to make an array of them. But also 
every menuitem has several atributes so every menuitem 
is also an object.

We get this:
"popup": {
    "menuitem": [
                {"value": "New", "onclick": "CreateNewDoc()"},
                {"value": "Open", "onclick": "OpenDoc()"},
                {"value": "Close", "onclick": "CloseDoc()"}
    ]
}
We will connect both above examples and now we get this: 
{
"menu": {
    "id": "file",
    "value": "File",
"number" : 50,
    "popup": {
      "menuitem": [
        {"value": "New", "onclick": "CreateNewDoc()"},
        {"value": "Open", "onclick": "OpenDoc()"},
        {"value": "Close", "onclick": "CloseDoc()"}
      ]
    }
  }
}

Wednesday, July 13, 2011

Intro to JSON

In this post I will talk about JSON and its basics. Since it is a main notation language to work with MongoDB.

JSON stands for JavaScript Object Notation.

Like XML JSON is a data interchange format, it is readable by humans and in some aspects better than xml. For example it is more compact and it saves traffic because it has small size whereas xml file can get very large and heavy to transfer over network.

JSON is completely language independent but it is developed on many language concepts that exist in C/C++, python, Java, Javascript, Perl etc...

JSON has two data structures built in.

- Collection of name/value pairs. In other langs this type of collection is realised as  object, record, struct, dictionary, hash table, keyed list, or associative array.

- Ordered list of values. In other langs this is an array, vector, list, or sequence.
These two data structures in JSON can get the form of: object, array, string, value and number.

- Object is an unordered set of name/value pairs. Object begins with "{" and ends with "}". Each name/value pair is separated with other pairs with "," and between name and value always stands ":".

example: object {
                      myName:Vlad,
                      myBlog:tunephp.blogspot.com
               }

-  Array is an ordered collection of values. Array starts with "[" and ends with "]". Values are separated by ",".

example: array[val1,val2,val3]

- Value can be of type string, number, object, array, true, false, null
- String can be any type of string like in other langs.
- Number is a usual number like in other langs (C, Java) except that octal and hexadecimal aren't used.

You might wanna visit the official site JSON . Also dont forget to visit the examples at JSON Examples