Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

AMF serialization/deserialization for JS

License

NotificationsYou must be signed in to change notification settings

infomaniac-amf/js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

66 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Status:Build Status

Cross-browser compatibility

browser support

Intro

AMF (Action Message Format) is a binary data serialization protocol. Simply put, it transforms objects in memory into a binary string, and can reverse this binary string into an object in memory. It can be used just likeJSON, and this library has been build to provide a similar API to that exposed by theJSON functionality inJavaScript.

Purpose

The purpose of this library is to provide a consistent and symmetric implementation of theAMF specification in bothPHP &JavaScript.

Why use AMF?

Well, it's up to you.JSON is perfectly suited to the web, however it does have some shortcomings which are addressed byAMF. For starters,JSON cannot handle complex object graphs with circular references; additionally, it cannot serialize dates & byte arrays - you would need to do some additional work to support these inJSON (convert date to unix timestamp, byte arrays to base64).

Should I stop using JSON?

Hells no.JSON is great;AMF can simply provide you with some additional functionality which could help you build your web app.

Getting Started

To begin using this library, you will need to install it viaComposer:

bower install -S infomaniac-amf.js#latest

This will download the repository for you and make the library available for inclusion. For convenience, a filedist/amf.js has been compiled for you with all of its dependencies, uglified and browserified. The file is a mere43kb.

<scriptsrc="bower_components/infomaniac-amf.js/dist/amf.js"type="text/javascript"></script>

Usage

Here is a simple example of encoding an object toAMF:

vardata={any:'data',you:'like!'};varencodedData=AMF.stringify(data);console.log(encodedData);

This will produce a binary string which represents your given data.

If you were to inspect the HTTP traffic of a page that producesAMF data, using a tool such asCharles Proxy, you would see the following output:

To decode this string, simply do the following:

vardata={any:'data',you:'like!'};varencodedData=AMF.stringify(data);console.log(AMF.parse(encodedData));

If you were toconsole.log this data, it would look identical to the input data given to theAMF.stringify function.

Object{any:"data",you:"like!"}

Extra Features

Class-mapping

AMF allows you to encode an object and retain some metadata about it; for example, when serializing an instance of a class (notObject) the library will retain the class' fully qualified namespace name and use it to reconstruct an object of that type upon decoding.

Consider the following class:

varPerson=function(){this.name='Bob';this._classMapping='Person';};

If we encode an instance of this object, by default its class type will be ignored and when the data is decoded, the resulting value will be a plain PHPObject instance.

varPerson=function(){this._classMapping='Person';};vardata=newPerson();data.name="Bob";varencodedData=AMF.stringify(data);console.log(AMF.parse(encodedData));

...will produce...

Object{name:"Bob"}

In order to retain the class type inAMF, you will need to add the following:

  1. an additional flag to theAMF.stringify function call
  2. define a_classMapping variable on the object(s) being encododed, and
  3. register a "class alias" usingAMF.registerClassAlias to associate the_classMapping value to its related class type
varPerson=function(){this._classMapping='Person';};vardata=newPerson();data.name="Bob";varencodedData=AMF.stringify(data,AMF.CLASS_MAPPING);AMF.registerClassAlias('Person',Person);console.log(AMF.parse(encodedData));

Now, when this data is decoded, the library will attempt to create a new instance of thePerson class and set its public propertyname to"Bob".

Person{_classMapping:"Person",name:"Bob"}

Data Encoding (Serialization)

TheAMF spec allows for the serialization of several different data-types.

Here is a link to the latest specification:AMF3 Spec - January 2013

This library implements10 of the18 data-types described in the specification. The reason for the support of only asubset of these types can be seen in two lights:utility andlimitation. Here is an exhaustive list of the data-types available:

Data-TypeIncludedReason for exclusion
Undefined-
Null-
False-
True-
Integer-
Double-
String-
XML DocumentWho needs XML?
Date-
Array-
Object-
XMLWho needs XML?
ByteArray-
VectorNot high priority - also, possible browser incompat issue with JS
VectorNot high priority - also, possible browser incompat issue with JS
VectorNot high priority - also, possible browser incompat issue with JS
VectorNot high priority - also, possible browser incompat issue with JS
DictionaryPHP cannot use objects are array keys

License

This project is licensed under theMIT license.

Acknowledgements

While writing this library, I used several libraries to validate my progress, and to help me come unstuck. I would like to extend a special thanks to the following libraries and individuals:

About

AMF serialization/deserialization for JS

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp