DEVNET-2006Coding 210: Parsing JSON in C++

55

Transcript of DEVNET-2006Coding 210: Parsing JSON in C++

Page 1: DEVNET-2006Coding 210: Parsing JSON in C++
Page 2: DEVNET-2006Coding 210: Parsing JSON in C++

Coding 210 Parsing JSON in C++

Mike Maas, Technical Evangelist – IoE, DevNet, @mike_maas

Page 3: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

•  Introductions •  Overview •  Requirements •  C++ Primer •  Requesting Content •  Parsing Content •  Resources

Agenda

3

Page 4: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

Introductions •  Who Am I?

•  Mike Maas, Technical Evangelist – Internet of Everything, DevNet

•  Why this session? Why C++? •  It is not going away

•  http://stackoverflow.com/research/developer-survey-2015#tech •  http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html •  http://langpop.com/ •  C > C++ > C++11 > C++14

•  Great for constrained applications •  Better performance and control of resources

4

Page 5: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

Overview

5

Page 6: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

Overview •  Goals

•  Build upon Coding 101 and Coding 201 •  Briefest introduction to C++ and application compilation •  Deeper understanding of using C++ to collect information from the network •  Further inspection of JSON and the tools available to evaluate JSON content •  Understand the methods and implementation of parsing JSON

•  What we won't be covering •  Many C/C++ Features, Integrated Development Environments (IDEs) and other tools •  Authentication

•  Oauth, Digest/Form/etc.

Page 7: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

Overview •  What we are going to work on

•  Using Postman •  Learning a little bit about CMX •  Understanding how to use C++ and useful libraries to parse data •  Understanding some of the challenges of parsing data •  Seeing some of the errors you might encounter

Page 8: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

Requirements

8

Page 9: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

Requirements: Tools •  A web browser (Chrome)

•  http://getchrome.com

•  C/C++ Compiler

•  Text Editor •  Git (Optional)

•  http://git-scm.com/

•  Postman (Optional) •  Get from the Chrome Web Store or the standalone version at

http://www.getpostman.com/

Page 10: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

Requirements: Expertise •  Basic computer skills / Accessing web sites

•  Using the command-line for your Operating System

•  Some skills in scripting or computer automation

Page 11: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

Requirements: OS X

•  Install the G++ C/C++ Compiler •  Install XCode •  Install Command Line Developer Tools for OS X

•  Open a Terminal •  Launch a terminal via Spotlight > Terminal •  Type g++ --version

•  We should get something like "Apple LLVM version 6.1.0" •  If we don't

•  On OS X : Verify you have XCode and the command line tools installed

Pre-Flight Check: C/C++ Compiler

Page 12: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

Requirements: Windows

•  Install Cygwin on your computer

•  Install the g++ C/C++ Compiler •  Run Cygwin, Install from Internet, •  Within the Cygwin setup, install the following:

•  gcc-g++, openssl-devel, libssh2-devel, libcurl-devel, libcurl4

•  Open a console •  Locate and start the Cygwin terminal •  Type g++ --version

•  We should get something like "g++ (GCC) 4.9.2" •  If we don't

•  Verify you are running it from a Cygwin terminal

Pre-Flight Check: C/C++ Compiler

Page 13: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

Requirements

•  Install Postman if you don't already have it installed •  This isn't required but it is a very useful tool •  Available at http://www.getpostman.com/ •  If you are used to a different tool (SoapUI, Chrome DevTools, etc.) – Feel free

•  Browser-based version or Standalone packaged application is fine

Pre-Flight Check: Postman

Page 14: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

Requirements: Code

14

•  GitHub •  https://github.com/CiscoDevNet/coding-skills-sample-code

•  coding210-parsing-json-c++

•  To Clone locally run the following command: •  git clone https://github.com/CiscoDevNet/coding-skills-sample-code.git •  Within our source directory we will also need to clone libcurl and RapidJSON

•  git clone https://github.com/bagder/curl.git •  git clone https://github.com/miloyip/rapidjson.git

All the samples used are available online

Page 15: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

Optional: Tools

15

•  JSON Test •  http://www.jsontest.com/

•  JSON Lint •  http://jsonlint.com/

•  JSON Placeholder •  http://jsonplaceholder.typicode.com/

Other online resources for our foray into JSON parsing

Page 16: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

C++ Primer

16

Page 17: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

C++ is a standardized, object-oriented programming language. It is designed for efficiency, performance, and flexibility of use.

What is C++?

17

Page 18: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

•  C/C++ has a notable barriers to entry •  Broad Syntax / Deep Feature Set

•  Pointers/References, Generics •  Object Oriented Conceptualization, Programming, and Design

•  Class > Object, Inheritance/Polymorphism, etc. •  Compilation

•  Many different compilation tools and toolchains •  http://en.wikipedia.org/wiki/List_of_compilers#C.2B.2B_compilers

•  GUIs are not it's primary concern •  Lots of Dragons and Grognards be there

C++ Primer

18

Page 19: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

•  Typed Programming Languages •  Static / Compile-Time / Strong

•  Ada, C, C++, C#, Java, Pascal, … •  Dynamic / Run-Time / Weak

•  JavaScript, Lisp, Lua, Python, Ruby, Perl, Smalltalk, … •  Optionally Both

•  Objective-C, TypeScript, …

•  Runtimes •  Java, Python, Common Language Runtime (CLR), etc use an interpreter executable

for a particular platform •  C/C++ Create a executable for a particular platform

C++ Primer – Compare and Contrast

19

Page 20: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

•  C# •  Compiles to Common Intermediate

Language (CIL) •  Uses the CLR •  Garbage Collected •  Platform includes Standard Libraries

•  Syntax Differences •  Types

•  Object versions of types •  long = 8 bytes

•  Control •  switch explicit

•  Inheritance •  …

•  C++ •  Compiles to a platform machine code

•  Not Garbage Collected •  Developer includes Standard Libraries

•  Syntax Differences •  Types

•  Fundamental types •  long = 4 bytes

•  Control •  switch falls through

•  Inheritance •  …

C++ Primer – Compare and Contrast

20

Page 21: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

•  To just get something to the screen… •  Create a syntactically correct source code file with a main entry point •  Compile that source code file •  Link the resultant object code into an Application •  Run the resultant Application

C++ Primer – Compare and Contrast : HelloWorld!

21

Page 22: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

• C# using System;

public class HelloWorld

{

static public void Main()

{

Console.WriteLine("Hello World!");

Console.WriteLine("How are you?");

}

}

• C++ #include <iostream>

int main()

{

std::cout << "Hello World!\n";

std::cout << "How are you?\n";

return 0;

}

C++ Primer – Compare and Contrast : HelloWorld!

22

Page 23: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

Activity 1 : Compilation

23

Page 24: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

Creating an application via C++

•  Get Code Via Git •  Git clone the following command:

•  git clone https://github.com/CiscoDevNet/coding-skills-sample-code.git

Let’s get something to look at in our console

•  Get Code Via Download •  Download the following file:

•  https://github.com/CiscoDevNet/coding-skills-sample-code/archive/master.zip

•  When you get a coding-skills-sample-code directory/folder •  Launch a terminal and navigate to coding-skills-sample-code •  Locate the coding210-parsing-json-c++ directory/folder •  We will execute commands directly from this folder

Page 25: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

Basic Construction via C++

25

.c .cpp

Compiler Linker

.lib .o

.dll .exe .lib

Source

Page 26: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

•  Source Files •  hello-world.cpp (valid C++ instructions)

•  Compilation (compiler, assembler, and linker) g++ -c hello-world.cpp -o hello-world.o

g++ -o hello hello-world.o

- or - g++ -o hello hello-world.cpp

•  Output •  hello (OS X), hello.exe (Windows)

Basic Construction

26

Page 27: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

Requesting Content

27

Page 28: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

Request and Response

•  Request •  What you want •  Parameters

•  Response results can often be controlled via Request elements •  URL query params •  Form-based transactions •  HTTP Headers •  Pseudo Extensions (https://stream.twitter.com/1.1/statuses/sample.json)

•  Response •  What you get •  Content

•  Can often return the same data in different formats per request •  XML, JSON, ATOM, …

What you want; what you get

Page 29: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

HTTP

•  What does HTTP stand for? (HT) & (TP) •  What is the most relevant part of the definition for us today?

•  Headers •  Authorization •  Accept

•  Charset, Encoding, Language •  Actions

•  GET – Actually get content •  POST – Create new content •  DELETE – Delete content •  PUT – Update content

•  Content Description •  MIME Types •  For HTTP the content is negotiated

A brief word about HTTP

Page 30: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

Requesting Content via Postman

30

•  From Postman •  In the Request field enter our test URL

•  http://jsonplaceholder.typicode.com/users •  Click Send

•  From a Browser •  Just put http://jsonplaceholder.typicode.com/users in as the URL

•  We should get some JSON returned

Let’s get something to look at

http://jsonplaceholder.typicode.com/users http://jsonplaceholder.typicode.com/todos

Page 31: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

Understanding the structure of the Response

[

{

"id" : 1,

"name": "Leanne Graham"

}, { "id": 2, "name": "Ervin Howell" }

]

Let's look at this in JSON Lint and then JSON.org.

Let’s look at what we got (Condensed)

Page 32: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

Understanding the structure of the Response

[ (Array)

{ (Object)

"id" : 1, (Value)

"name": "Leanne Graham" (Value)

}, { (Object) "id": 2, (Value) "name": "Ervin Howell" (Value) }

]

Let’s look at what we got (Condensed)

Page 33: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

Activity 2: Requesting Content

Page 34: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

•  What needs to happen?

•  Establish a HyperText Transport Protocol (HTTP) connection •  Sockets •  Network Library

•  Send and Receive data against the HyperText Transport Protocol (HTTP) •  Synchronous/Blocking or Asynchronous/Non-Blocking

•  Parse and handle the Response data

Requesting Content via C++

34

Page 35: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

•  Get the libcurl source •  http://curl.haxx.se/dev/source.html •  Execute the following command in your source directory:

•  git clone https://github.com/bagder/curl.git

•  Use the cURL functionality •  Include headers to add libcurl support •  Use the cURL Easy interface (Synchronous) - or - •  Use the cURL Multi interface (Asynchronous)

Requesting Content via C++ using libcurl

35

Page 36: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

•  A basic HTTP application •  main-http.cpp

•  Code available at https://github.com/CiscoDevNet/coding-skills-sample-code

•  Compile the main-http application •  g++ -o main-http main-http.cpp –lcurl

•  How do we know what these flags are? •  curl-config

•  Evaluate the returned results

Requesting Content via C++ using libcurl

36

Page 37: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

Activity 3: Requesting JSON

Page 38: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

•  A basic HTTP application that returns JSON •  main-json.cpp

•  Code available at https://github.com/CiscoDevNet/coding-skills-sample-code

•  Compile the main-http application •  g++ -o main-json main-json.cpp –lcurl

•  Evaluate the returned results

Requesting JSON via C++ using libcurl

38

Page 39: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

Parsing Content

39

Page 40: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

•  Why Parse? •  Methods of Parsing

•  Don't Parse •  Output is truly human-readable

•  Do-It-Yourself (DIY) String Parsing •  Useful for really, really simple string extraction

•  Build or Use a Framework •  A lot of ready-made libraries and tools are already available

Parsing Content

40

Page 41: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

•  Available Libraries •  jsoncpp •  rapidjson •  LibJSON •  JSON++ •  …

•  JSON.org has lists of utilities at http://json.org/

Methods of Parsing Content

41

Page 42: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

Activity 4: Parsing Content

Page 43: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

•  What needs to happen?

•  Need to establish a Data source •  Identify the Data to extract

•  Walk the String •  Regular Expressions •  Parsing Library

•  Extract the Data •  Instance Counts •  Copy of the Data itself

•  Do something with the Data

Parsing Content via C++

43

Page 44: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

•  Get the RapidJSON source •  https://github.com/miloyip/rapidjson •  Execute the following command in your source directory:

•  git clone https://github.com/miloyip/rapidjson.git

•  Use the RapidJSON functionality •  Include headers to add rapidjson support

Parsing Content via C++ using RapidJSON

44

Page 45: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

•  An application that pretties the JSON output •  main-json-lib

•  Code available at https://github.com/CiscoDevNet/coding-skills-sample-code

•  Compile the main-http application •  g++ -o main-json-lib main-json-lib.cpp -Irapidjson/include –lcurl

•  Do something with the returned results •  Like Prettify!

Parsing Content via C++ using RapidJSON

45

Page 46: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

Activity 5: Extracting Content

Page 47: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

•  An application that extracts data from the JSON output •  main

•  Code available at https://github.com/CiscoDevNet/coding-skills-sample-code

•  Compile the main-http application •  g++ -o main main.cpp -Irapidjson/include –lcurl

•  Do something with the returned results •  Like Get AccessPoint names from your CMX installation!

Parsing Content via C++ using RapidJSON

47

Page 48: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

Demo: IOx

48

Page 49: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

•  IOx •  Edge Computing

•  C++ on the Edge •  Cross-Compilation Toolchain

•  x86 > PowerPC

Demonstration

49

Page 50: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

Resources

Page 51: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

Resources

•  On-Site •  Learning Labs

•  https://learninglabs.cisco.com •  DevNetizens

Information to help you on your way

Page 52: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

Resources

•  On-Line •  Learning Labs

•  https://learninglabs.cisco.com •  C++

•  http://www.cplusplus.com/ •  http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/

•  JSON : JSON.org •  JSON Test : http://www.jsontest.com/ •  JSON Lint : http://jsonlint.com/ •  JSON Placeholder : http://jsonplaceholder.typicode.com/

Information to help you on your way

Page 53: DEVNET-2006Coding 210: Parsing JSON in C++

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Coding 210: Parsing JSON in C++

•  Try it yourself •  Do a Learning Lab •  Go Online •  Read a book

Where to go from here?

53

Page 54: DEVNET-2006Coding 210: Parsing JSON in C++

Thank you

© 2015 Cisco and/or its affiliates. All rights reserved. Cisco Public Presentation ID 54

Page 55: DEVNET-2006Coding 210: Parsing JSON in C++