Showing posts with label json. Show all posts
Showing posts with label json. Show all posts

Monday, June 28, 2021

[Java - Spring Boot] วิธีการสร้าง JSON response สำหรับทุก controller ใน Spring Boot
[Java - Spring Boot] How to Wrap JSON Response for All Controllers in Spring Boot

หลังจากเรากำหนด @RestController ที่เหนือ class ของ controller ทำให้ object ที่ return จาก controller นั้นจะถูกแปลงไปเป็น JSON response ให้โดยอัตโนมัติ

อย่างไรก็ตาม ถ้าหากเราต้องการสร้าง template ครอบ object ที่ return จาก controller ทุกๆ controller ก่อนที่จะส่งเป็น JSON response ออกไป เรามีขั้นตอนดังนี้

  1. สร้าง Class สำหรับ JSON response template
    package com.example.model.wrapper;
    
    import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
    import com.fasterxml.jackson.annotation.JsonInclude;
    import com.fasterxml.jackson.annotation.JsonProperty;
    import org.springframework.http.HttpStatus;
    
    @JsonIgnoreProperties(ignoreUnknown = true)
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public class ResultWrapper<T> {
        @JsonProperty("code")
        private Integer code;
        @JsonProperty("status")
        private HttpStatus status;
        @JsonProperty("message")
        private String message;
        @JsonProperty("data")
        private T data = null;
    
    
        public Result(Integer code, HttpStatus status, String message) {
            this.code = code;
            this.status = status;
            this.message = message;
        }
    
        public ResultWrapper(Integer code, HttpStatus status, T data) {
            this.code = code;
            this.status = status;
            this.data = data;
        }
    }
    

Thursday, March 18, 2021

[NodeJS] วิธีการนำเข้าไฟล์ JSON ไปยังตัวแปรและการส่งออกค่าตัวแปรไปยังไฟล์ JSON (การอ่านและเขียนไฟล์ JSON)
[NodeJS] How to Import JSON File to Variable and Export Variable to JSON File (Reading and Writing JSON file to/from Variable)

สมมติว่า เรามีไฟล์ JSON อยู่ที่ ./resources/symbols.json และเราจะใช้ตัวแปรชื่อ symbols ในการเก็บค่าที่อ่านได้จากไฟล์ JSON  
เราสามารถอ่านไฟล์ JSON แล้วเก็บค่าเข้าตัวแปรได้ดังนี้
const symbols = require('./resources/symbols.json');
ในทางกลับกัน ถ้าเราต้องการเขียนค่าของตัวแปรออกมาเป็นไฟล์ JSON เราจะใช้วิธีดังนี้
const fs = require('fs');
const jsonData = JSON.stringify(symbols, null, 4); // third parameter defines white-space insertion for pretty-printing
fs.writeFileSync('./resources/symbols.json', jsonData);

Friday, April 10, 2020

[Laravel] การบันทึกไฟล์ JSON ใน Laravel Tinker
[Laravel] How to Save JSON File in Laravel Tinker

Tinker ช่วยให้เราสามารถตอบโต้กับ Laravel application ของเราทั้งหมดผ่านทาง command line ได้

เราสามารถเข้าใช้ Tinker ได้ โดยรันคำสั่งดังนี้
php artisan tinker
ส่วนใหญ่เราจะใช้ Tinker ช่วยในการตรวจสอบความถูกต้องของข้อมูลในฐานข้อมูล โดยเราจะใช้ Eloquent ในการ query ข้อมูลขึ้นมา และในบางครั้งเราต้องการส่งออกผลการ query ออกเป็นไฟล์ เพื่อนำไปตรวจสอบเพิ่มเติม

สมมติว่า เรา query ข้อมูลใน Tinker แล้วเก็บใส่ตัวแปรดังนี้
$users = User::all();
และเรามีการตั้งค่า disks ในไฟล์ config/filesystems.php ดังนี้
...
    'disks' => [
        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],
...
การส่งออกผลการ query ข้างต้นออกเป็นไฟล์ JSON เราจะรันคำสั่งใน Tinker ดังนี้
Storage::disk('local')->put('users.json', json_encode((array) $users, JSON_PRETTY_PRINT));
ไฟล์ผลการ query ที่ถูกสร้างอยู่ที่ path storage/app/users.json