Giter VIP home page Giter VIP logo

zvjson-library's Introduction

ZVJson Library Documentation

The ZVJson library is a versatile utility that simplifies JSON serialization and deserialization, HTTP requests, and image loading in Unity projects. This comprehensive documentation provides a guide on how to effectively integrate and use the ZVJson library within your Unity applications.

Table of Contents

  1. Introduction
  2. Installation
  3. JSON Serialization and Deserialization
  4. HTTP Requests
  5. Loading Images from URLs
  6. Examples
  7. Error Handling and Status Codes
  8. Understanding useJsonFixer

1. Introduction

The ZVJson library is designed to simplify common tasks related to JSON handling, HTTP requests, and image loading in Unity projects. Its set of utility methods allows developers to focus on the core logic of their applications while seamlessly integrating data manipulation and network communication.

2. Installation

To integrate the ZVJson library into your Unity project:

  1. Download the provided ZVJson.cs file.
  2. Place the downloaded ZVJson.cs file into a designated folder within your Unity project, such as Assets/Scripts.
  3. Unity will automatically include and compile the library as part of your project.

3. JSON Serialization and Deserialization

The ZVJson library provides methods to facilitate JSON serialization and deserialization. These methods are particularly useful when working with JSON data from APIs or other sources.

Deserializing JSON

// Deserialize JSON data into an array of objects
YourObjectType[] objects = ZVJson.FromJson<YourObjectType>(jsonString, useJsonFixer);

Serializing JSON

// Serialize an array of objects into a JSON string
string jsonString = ZVJson.ToJson(objects);

4. HTTP Requests

The ZVJson library simplifies making HTTP GET requests and handling the responses.

Sending a GET Request

StartCoroutine(ZVJson.GetRequest(url, (statusCode, response) =>
{
    if (statusCode == 4)
    {
        // Process the response data
    }
    else
    {
        // Handle the error
        HandleError(statusCode);
    }
}));

5. Loading Images from URLs

The ZVJson library includes features to efficiently load images from URLs and convert them into Unity sprites.

StartCoroutine(ZVJson.GetImageFromUrl(imageUrl, (statusCode, sprite) =>
{
    if (statusCode == 4)
    {
        // Use the loaded sprite
    }
    else
    {
        // Handle the error
        HandleError(statusCode);
    }
}));

6. Examples

Combining GET Request and FromJson

Assuming you're working with a JSON API that provides data about books:

// Load and process books from a JSON API
StartCoroutine(ZVJson.GetRequest(apiUrl, (statusCode, response) =>
{
    if (statusCode == 4)
    {
        BookData[] books = ZVJson.FromJson<BookData>(response, true);
        foreach (BookData book in books)
        {
            Debug.Log($"Title: {book.title}, Author: {book.author}, Page Count: {book.pageCount}");
        }
    }
    else
    {
        // Handle the error
        HandleError(statusCode);
    }
}));

Creating a Type Matching JSON Data

Create a C# class that matches the JSON data structure:

[System.Serializable]
public class BookData
{
    public string title;
    public string author;
    public int pageCount;
}

By creating a matching class, you can easily deserialize JSON data into instances of this class.

7. Error Handling and Status Codes

The ZVJson library provides status codes to help you understand the result of your operations:

  • Status Code 1: Connection Error
  • Status Code 2: Data Processing Error
  • Status Code 3: Protocol Error
  • Status Code 4: Success

You can use these status codes to handle errors and unexpected behaviors gracefully in your application.

8. Understanding useJsonFixer

The useJsonFixer parameter in the FromJson method is used to handle a common issue when deserializing JSON arrays with a single root object. When set to true, the library automatically adds a wrapper object to the JSON string before deserialization, ensuring proper parsing of the JSON array.

  • useJsonFixer = true: The library adds a wrapper object to the JSON before deserialization.
  • useJsonFixer = false: The JSON is assumed to have a valid structure, and no additional modification is made.

Use useJsonFixer as needed based on the JSON structure you are working with.

Conclusion

The ZVJson library streamlines JSON handling, HTTP requests, and image loading tasks in Unity projects. By following this documentation, you're well-equipped to integrate and leverage the ZVJson library to enhance your Unity application's functionality. For additional guidance or troubleshooting, refer back to this documentation or consult the Unity community.

zvjson-library's People

Contributors

zakarialouqdyeme avatar

Stargazers

Maddy avatar Elmissouri avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.