BSc (Hons) Software Engineering - Web Service Development Past Paper PDF - University of Technology, Mauritius 2024
Document Details
Uploaded by RecordSettingCarnelian2276
University of Technology, Mauritius
2024
Tags
Summary
This document provides an overview of JSON, its characteristics, and its use in modern web applications. It also details the advantages of using JSON over XML for web development and describes how JSON is used in different programming languages and frameworks.
Full Transcript
BSc (Hons) Software Engineering Module Name: Web Service Development Module Code: WAT 2104C University of Technology, Mauritius NOVEMBER 2024 Content Overview of JSON Data Format Practical exercises: Creating/Validating/Parsing JSON JSON: JavaScript Object Notation JSON's simplicity, lightwei...
BSc (Hons) Software Engineering Module Name: Web Service Development Module Code: WAT 2104C University of Technology, Mauritius NOVEMBER 2024 Content Overview of JSON Data Format Practical exercises: Creating/Validating/Parsing JSON JSON: JavaScript Object Notation JSON's simplicity, lightweight structure, native compatibility with JavaScript, cross-language support, flexibility, and efficient data handling make it an ideal choice for APIs and data exchange in modern software applications. Its adoption has made it the standard for most RESTful web services, providing a universal format that streamlines communication between clients and servers across the internet. Characteristics 1. Simplicity and Readability Human-Readable: JSON is designed to be easy for humans to read and write. Its syntax is straightforward, using a lightweight text-based structure that resembles key-value pairs in a dictionary. Minimal Syntax: JSON uses minimal formatting, typically requiring only curly braces {}, square brackets [], colons :, and commas ,. This simplicity allows developers to quickly interpret and understand the data structure without extensive documentation. Characteristics 2. Lightweight and Efficient Compact Data Representation: JSON is a relatively lightweight format, with minimal overhead in terms of syntax. This leads to smaller file sizes, which is especially valuable in web applications where data is transferred over networks. Fast Parsing and Processing: JSON is easy to parse and generate, which reduces processing time and resources on both the client and server sides. This is crucial for APIs that need to handle high traffic or work on resource-constrained devices. Characteristics 3. Native Compatibility with JavaScript JavaScript Object Notation: JSON was derived from JavaScript object syntax, making it natively compatible with JavaScript. This allows JSON data to be seamlessly integrated into web applications, especially for JavaScript-based front-end frameworks. Direct Usage in Web Browsers: Because web browsers are built with JavaScript engines, JSON data can be directly parsed and used in web applications, eliminating the need for complex parsing mechanisms or data transformations. Characteristics 4. Language Agnosticism and Interoperability Cross-Language Support: JSON is not tied to any specific programming language and is widely supported across various languages like Python, Java, C#, Ruby, and more. Almost all modern programming languages provide libraries for JSON parsing and generation. Interoperability Between Systems: JSON allows different systems (written in different languages or frameworks) to communicate with each other effectively. This cross-platform compatibility makes JSON ideal for microservices and distributed systems, where multiple components need to share data seamlessly. Characteristics 5. Widely Used in RESTful APIs Default Format for REST APIs: JSON has become the default choice for RESTful APIs, as it enables easy data exchange between clients and servers over HTTP. REST APIs often use JSON as the payload format, allowing front-end applications (like mobile apps or web apps) to communicate easily with back-end services. Standard for Web Applications and Mobile Apps: JSON is well-suited for web and mobile applications, which need to fetch data from remote servers. JSON’s lightweight nature is ideal for environments where bandwidth and processing power may be limited. Characteristics 6. Flexibility in Data Structure Support for Complex Data Types: JSON can represent complex data structures using nested objects and arrays. This allows APIs to send structured data, like lists of objects or hierarchical data, within a single JSON document. Dynamic Schema: Unlike XML, which often relies on strict schemas, JSON is more flexible and does not require rigid structure. This allows developers to modify or expand JSON structures without needing to redefine schema constraints, which is beneficial for APIs that need to evolve over time. Characteristics 7. Reduced Overhead Compared to XML Less Verbose: JSON’s syntax is less verbose than XML, which uses opening and closing tags for every element. This makes JSON files smaller and faster to transmit, saving bandwidth and reducing latency. Simpler Structure Without XML’s Complexity: JSON doesn’t require additional specifications like XML namespaces, DTDs (Document Type Definitions), or CDATA sections, making it easier to work with, especially for quick data exchange in web applications. Characteristics 8. Growing Ecosystem and Adoption Wide Adoption in Modern Frameworks: Many modern development frameworks, libraries, and tools are built to work with JSON by default, including front-end JavaScript frameworks (like React, Angular, and Vue), back-end frameworks (like Node.js and Django), and mobile development frameworks (like Swift and Kotlin). Standardized Tooling: With extensive support for JSON in REST clients, server frameworks, debugging tools, and data stores (like NoSQL databases), developers have a well-established ecosystem for JSON. Comparison JSON/XML XML JSON 1. Readability 1. Readability { John Doe "name": "John Doe", "age": 30 30 } 2. Size 2. Size XML’s use of closing tags for each element adds extra JSON has a more compact syntax since it doesn’t characters, making XML documents larger than equivalent require end tags for each key-value pair, reducing data JSON documents. Additionally, XML often includes metadata size. and namespaces, which add even more to its size. This is especially important when data is transmitted over networks, as smaller file sizes lead to faster transmission and lower bandwidth usage. Alice25 {"name": "Alice", "age": 25} Comparison JSON/XML XML JSON 3. Ease of Parsing 3. Ease of Parsing const data = JSON.parse('{"name": "Alice", "age": 25}’); const parser = new DOMParser(); (javascript) const xmlDoc = parser.parseFromString("Alice", "text/xml"); (javascript) Hands on Exercises 1. Reading a JSON as a string 2. Parsing string to JSON 3. Parsing JSON to C# objects 4. Using external libraries (E.g. Newtonsoft.Json) To use Newtonsoft.Json, add it to your project via NuGet. In Visual Studio, follow these steps: 1.Go to Project > Manage NuGet Packages. 2.Search for Newtonsoft.Json. 3.Install the latest version. 4. Using external libraries (E.g. Newtonsoft.Json) (Guided exercise) We will create the methods: LaunchExercise4 GetReadJsonFileFromFilePath DisplayCountryJson AddRecordToJson Objective: Using different libraries and distinguish between them Hands-on objects, Deserialise string to json & add additional records to objects Validating your JSON – Add your custom try catch Popular Online Validators: https://jsonlint.com/ https://jsonformatter.curiousconcept.com/ Or just use your IDE features (E.g. In visual studio code) 5. Handling unexpected data types with validation