Skip to content

Instantly share code, notes, and snippets.

@fdln
Created July 25, 2017 07:24
Show Gist options
  • Save fdln/ad9111a474cf38c7f9560fc2ff03e413 to your computer and use it in GitHub Desktop.
Save fdln/ad9111a474cf38c7f9560fc2ff03e413 to your computer and use it in GitHub Desktop.

JSON Data Type

Format Json data yang akan di consume Mobile App. Value untuk null digantikan ke empty value untuk masing-masing tipe datanya.

Type Acceptable Value Empty Value (null)
string "Lorem ipsum"

"Dolor \nSit amet"
""
integer 42
-42
0
float 1.234
-12.345
0
boolean true
false
false
object {"lorem":"Lorem Ipsum", "dolor":"Dolor Sit Amet"} {}
array ["a", "b", "c"]

[ 1, 2, 4, 5]
[]
array of object [{"key":"Lorem Ipsum"}, {"key":"Dolor sit amet"}]

[{"x":32, "y":23},{"x":10, "y":0},{"x":11, "y":11}]
[]

Error code and message

Sample code and message

code message
404 Not found
204 Success with no content

Sample JSON Response

Error Response

Response tidak mengembalikan data pada json elemen 'data'(set ke empty object {})

{
  "api_version": "2.0",
  "memory_usage": "3.31MB",
  "elapse_time": "6.267460107803345",
  "lang": "id",
  "currency":"IDR",
  "error": {
    "code": 404,
    "message": "File Not Found",
    "errors": [
      {
        "code": 3,
        "reason": "ResourceNotFoundException",
        "message": "File Not Found"
      },{
        "code": 7,
        "reason": "Invalid format",
        "message": "Format tanggal lahir salah"
      }

    ]
  },
  "data": {}
}

Success with Error Response

{
  "api_version": "2.0",
  "memory_usage": "3.31MB",
  "elapse_time": "6.267460107803345",
  "lang": "id",
  "currency":"IDR",
  "error": {
    "code": 299,
    "message": "Expired Token",
    "errors": []
  },
  "data": {
    "updated": "2010-01-07T19:58:42.949Z",
    "total_items": 800,
    "start_index": 1,
    "items_per_page": 1,
    "items": [
      {
        "id": "hYB0mn5zh2c",
        "uploaded": "2007-06-05T22:07:03.000Z",
        "updated": "2010-01-07T13:26:50.000Z",
        "title": "Google Developers Day US - Maps API Introduction",
        "description": "Google Maps API Introduction ...",
        "tags": [
          "GDD07",
          "GDD07US",
          "Maps"
        ],
        "thumbnail": {
          "default": "http://i.ytimg.com/vi/hYB0mn5zh2c/default.jpg",
          "hqDefault": "http://i.ytimg.com/vi/hYB0mn5zh2c/hqdefault.jpg"
        },
      }
    ]
  }
}

Success Response

Untuk response success, value untuk error di set ke empty object {}

{
  "api_version": "2.0",
  "memory_usage": "3.31MB",
  "elapse_time": "6.267460107803345",
  "lang": "id",
  "currency":"IDR",
  "error": {},
  "data": {
    "updated": "2010-01-07T19:58:42.949Z",
    "total_items": 800,
    "start_index": 1,
    "items_per_page": 1,
    "items": [
      {
        "id": "hYB0mn5zh2c",
        "uploaded": "2007-06-05T22:07:03.000Z",
        "updated": "2010-01-07T13:26:50.000Z",
        "uploader": "GoogleDeveloperDay",
        "category": "News",
        "title": "Google Developers Day US - Maps API Introduction",
        "description": "Google Maps API Introduction ...",
        "tags": [
          "GDD07",
          "GDD07US",
          "Maps"
        ],
        "thumbnail": {
          "default": "http://i.ytimg.com/vi/hYB0mn5zh2c/default.jpg",
          "hqDefault": "http://i.ytimg.com/vi/hYB0mn5zh2c/hqdefault.jpg"
        }
      }
    ]
  }
}

Success with no content

{
  "api_version": "2.0",
  "memory_usage": "3.31MB",
  "elapse_time": "1.23s",
  "lang": "id",
  "currency":"IDR",
  "error": {
    "code": 204,
    "message": "no content",
    "errors": []
  },
  "data": {}
}

How to Create "elapse_time"

<?php
$msc = microtime(true);
$temp_mmr = memory_get_usage();
try{

  /*
      your function start here
  */


  $msc=microtime(true)-$msc;
  $temp_mmr = memory_get_usage()-$temp_mmr;
	$memoryusage = $this->memory_usage($temp_mmr);

  //put $msc and $memoryusage in $response

  $response = array( .... );

  return Response::json($response); //this syntax is using laravel 4.2, kindly find in 5.^ if there is other syntax
}catch(Exception $e){
  $msc=microtime(true)-$msc;
  $temp_mmr = memory_get_usage()-$temp_mmr;
	$memoryusage = $this->memory_usage($temp_mmr);

  //put $msc and $memoryusage in $response

  $response = array( .... ); //put hardcode response here with error code and error message that already agree with Mobile dev

  return Response::json($response); //if using laravel 4.2 kindly find in 5.^ if there is other syntax
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment