• SEARCH
  • DOWNLOADS
  • MY ACCOUNT
  • Buy (0)
  • CONTACT
  • Free Trial
Wowza Logo
  • PRODUCTS
  • DEVELOPER
  • SOLUTIONS
  • PRICING
  • RESOURCES
  • SUPPORT
  • My Account
  • Buy (0)
  • SEARCH
  • Ask a question
  • Forums
    • Wowza ClearCaster
    • Wowza Streaming Engine
    • Wowza Streaming Cloud
    • Wowza Player
    • Wowza GoCoder SDK
    • Wowza Developer Dojo
    • Topics
    • Questions
    • Articles
    • Users
    • Badges
  • Sign in
  • Community Home /
  • Wowza Developer Dojo /
  • Wowza Streaming Server Java API /
avatar image
Question by Chris Rogus · May 07, 2010 at 10:38 AM · wowza streaming server java api

JSON encoding of AMFData

I was trying to store AMFData in a db for later resending recently and as part of that I successfully experimented with JSON encoding for storage and transfer. I ended up using the builtin AMFDataObj.serialize and base64-ing it to avoid charset encoding issues with the bits when transferring it around: http://www.wowza.com/forums/showthread.php?p=41720#post41720 , but the JSON encoding did work first (it just took up more space in my db than base64 of raw AMF bytes). Here is the base AMFData JSON encoding function for the benefit of all. It does work to some degree at least, as it did work for my purposes (layered AMFDataArrays in AMFDataObjects and lots of numbers and strings in AMFDataItems), but please post fixes if you come across any bugs in using it, thanks.

// json serialization of wowza amfdata
public static String jsonAMF (AMFData d) throws Exception
{
	StringBuffer sb = new StringBuffer();
	int t = d.getType();

	if (t == AMFData.DATA_TYPE_STRING || t == AMFData.DATA_TYPE_DATE)
	 { sb.append("\"").append(jsonEscape(((AMFDataItem)d).toString())).append("\""); }

	else if (t == AMFData.DATA_TYPE_NUMBER || t == AMFData.DATA_TYPE_BOOLEAN) { sb.append(d); }

	else if (t == AMFData.DATA_TYPE_UNDEFINED || t == AMFData.DATA_TYPE_UNKNOWN
	 || t == AMFData.DATA_TYPE_NULL) { sb.append("null"); }

	// Mixed Array is not a perfect match:
	//  JSON only has string keys, whereas this allows integer keys as well as strings
	else if (t == AMFData.DATA_TYPE_OBJECT || t == AMFData.DATA_TYPE_MIXED_ARRAY)
	{
	        AMFDataObj o = (AMFDataObj)d;
		sb.append("{");
		Iterator i = o.getKeys().iterator();
		boolean notfirst = false;
		while (i.hasNext())
		{
			if (notfirst) { sb.append(","); }
			 else { notfirst = true; }
			String k = (String)i.next();
			sb.append("\"").append(jsonEscape(k)).append("\":");
			sb.append(jsonAMF(o.get(k)));
		}
		sb.append("}");
	}

	else if (t == AMFData.DATA_TYPE_ARRAY)
	{
	        AMFDataArray o = (AMFDataArray)d;
		sb.append("[");
		int i = 1, z = o.size();
		if (z > 0) { sb.append(jsonAMF(o.get(0))); }
		for (; i < z; i++) { sb.append(",");
		 sb.append(jsonAMF(o.get(i))); }
		sb.append("]");
	}

	else { throw new Exception("invalid amf data for json encode! (" + t + ")"); }

	return sb.toString();
}


The jsonEscape function in the code above swaps out invalid json string chars with their escaped versions as per http://json.org/string.gif and is basic but long so I won't post it.

Since I didn't want to write a JSON decoding function into AMFData, I just did the json decoding into basic objects on the flash end, sending only the json string data. The decoder comes from adobe labs hosted on google code:

http://code.google.com/p/as3corelib/

For those using the command line flex compiler mxmlc include this with (as3corelib.swc is in the lib dir of the as3corelib.zip you get from the website):

mxmlc -library-path as3corelib.swc ....


And in the flash actionscript, it's very simple:

import com.adobe.serialization.json.JSON;  // [url]http://code.google.com/p/as3corelib/[/url]
private function deserialize (jsonData:String) :void { process(JSON.decode(jsonData)); }


Enjoy!
Comment

People who like this

0 Show 0
10 |600 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

User Authentication 7 Answers

Server timestamp as ID3 to JWplayer client? 3 Answers

List chunks on an HLS live stream 3 Answers

TV Channel or TV Station 1 Answer

bypass Secure token for some client 1 Answer

Hot Topics
  • AWS Hosting
  • Mobile SDK
  • Deployment Options
  • Load Balancing
  • Content Security
Product Sign-in
  • Wowza Streaming Cloud
  • Wowza Player
Under the Hood
  • Developer Tools
  • Wowza System Status
  • Test Players
  • Developer IDE
Resellers
  • Find a Reseller
  • Reseller Portal
  • Become a Reseller
Company
  • About Us
  • Blog
  • News
  • Events
  • Careers
  • Customers
  • Partners
  • Contact Us
Stay Connected
Get Monthly Newsletter
Select a Language
  • English
    • English
    • Español
    • 日本語
    • 한국어
    • हिन्दी भाषा
    • 中文
    • русский язык

© 2005–2019 Wowza Media Systems, LLC. All rights reserved.   Terms | Privacy | Trademarks | Legal


Enterprise
Social Q&A

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Create an article
  • Forums
  • Wowza ClearCaster
  • Wowza Streaming Engine
  • Wowza Streaming Cloud
  • Wowza Player
  • Wowza GoCoder SDK
  • Wowza Developer Dojo
  • Explore
  • Topics
  • Questions
  • Articles
  • Users
  • Badges