Skip to content

Instantly share code, notes, and snippets.

@am
Created January 23, 2013 07:41
Show Gist options
  • Save am/4602893 to your computer and use it in GitHub Desktop.
Save am/4602893 to your computer and use it in GitHub Desktop.
Removing flex libs from the @darscan JSONClient class. Source: https://github.com/darscan/robotlegs-extensions-Oil/blob/master/src/org/robotlegs/oil/rest/JSONClient.as This works from FP 11.
//------------------------------------------------------------------------------
// Copyright (c) 2011 the original author or authors. All Rights Reserved.
//
// NOTICE: You are permitted you to use, modify, and distribute this file
// in accordance with the terms of the license agreement accompanying it.
//------------------------------------------------------------------------------
package org.robotlegs.oil.rest
{
import flash.utils.describeType;
public class JSONClient extends RestClientBase
{
/*============================================================================*/
/* Constructor */
/*============================================================================*/
public function JSONClient(rootURL:String = "")
{
super(rootURL);
addParamsTransform(paramsTransform);
addResultProcessor(resultProcessor);
}
/*============================================================================*/
/* Public Functions */
/*============================================================================*/
public function isPrimitive(source:*):Boolean
{
return source is String ||
source is Number ||
source is uint ||
source is int ||
source is Boolean ||
source == null;
}
/*============================================================================*/
/* Protected Functions */
/*============================================================================*/
protected function paramsTransform(source:*):Object
{
if (source == null)
return null;
if (isPrimitive(source))
return { value: source };
if (source is Array)
return { value: JSON.parse(source)};
source = source.valueOf();
var property:String;
var value:Object;
var classInfo:XML = describeType(source);
if ([email protected]() == "Object")
{
// Dynamic Object
for (property in source)
{
value = source[property];
if (value is Function || isPrimitive(value))
continue;
source[property] = JSON.stringify(value);
}
}
else
{
// Sealed Object
for each (var v:XML in classInfo..*.(
name() == "variable" ||
(
name() == "accessor" &&
attribute("access").charAt(0) == "r")
))
{
if (v.metadata && v.metadata.(@name == "Transient").length() > 0)
continue;
property = [email protected]();
value = source[v.@name];
if (value is Function || isPrimitive(value))
continue;
source[property] = JSON.stringify(value);
}
}
return source;
}
protected function resultProcessor(data:*, callback:Function):void // NO PMD
{
// Hack:
if (data == '[]')
{
callback(null, new Array());
return;
}
if (data == '')
{
callback(null, null);
return;
}
callback(null, JSON.parse(String(data)));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment