Skip to content

Instantly share code, notes, and snippets.

View dhiraj's full-sized avatar

Dhiraj Gupta dhiraj

View GitHub Profile
val titleString = root.pointer.at("/0/title").safeString
assertEquals("delectus aut autem",titleString)
val completed = root.pointer.at("/3/completed").safeBoolean
assertTrue(completed)
...
if(root is JsonArray && root.size() > 0){ //Smart cast 1
val firstElem = root[0]
if(firstElem is JsonObject){
val title = firstElem.get("title") //Smart cast 2
if (title != null && title is JsonPrimitive && title.isString){ //Smart cast 3
val titleString = title.asString
assertEquals("delectus aut autem",titleString)
}
val completed = firstElem.get("id")
@Test
fun getTodos(){
OkHttpClient().newCall(Request.Builder().url("https://jsonplaceholder.typicode.com/todos").build()).enqueue(object : Callback {
override fun onFailure(call: Call?, e: IOException?) {} //Handle Network failure
override fun onResponse(call: Call?, response: Response?) {
response!!.body()!!.use {responsebody ->
val root = JsonParser().parse(responsebody.charStream())
assertTrue(root is JsonElement) //root is a JsonElement object
assertTrue(root is JsonArray) //root is actually, a JsonArray object
### Keybase proof
I hereby claim:
* I am dhiraj on github.
* I am dhiraj (https://keybase.io/dhiraj) on keybase.
* I have a public key ASCtWqmgDKyi-1-GaEDW6W_MdXHA0xb-azCAZ3JybzxVAAo
To claim this, I am signing this object:
//
// KeyPath.swift
// GIFMosCore
//
// Copied liberally by Dhiraj Gupta on 4/27/17 from https://oleb.net/blog/2017/01/dictionary-key-paths/
//
import Foundation
public struct KeyPath {
### Keybase proof
I hereby claim:
* I am dhiraj on github.
* I am dhiraj (https://keybase.io/dhiraj) on keybase.
* I have a public key whose fingerprint is B1A2 1616 C66E CD49 F6E8 0582 3A81 C9D5 1C4B 8B39
To claim this, I am signing this object:
@dhiraj
dhiraj / AlbumRatingReset.scpt
Last active April 24, 2018 19:30
This AppleScript may allow you to remove or set the album rating on one or more of your tracks to a value you decide. If you use Smart Playlists based on the track rating field, this may be *the* AppleScript you were looking for. If not, then consider moving on, this is probably not what you were looking for. I've modified the script in this fil…
(*
"Album Rating Reset" for iTunes
written by Doug Adams
dougadams@mac.com
v1.0 sept 6 2007
-- initial release
v2.0 mar 6 2013
updated by Dhiraj Gupta (http://www.dhirajgupta.com)