Click here to Skip to main content
15,881,248 members
Articles / Programming Languages / PHP

Passing parameters between C++, PHP, JavaScript, etc...

Rate me:
Please Sign up or sign in to vote.
4.25/5 (9 votes)
2 Nov 2006CPOL9 min read 85.9K   565   39  
Easy to implement minimal format for simple data exchange, especially between new and obscure scripting languages.
/*------------------------------------------------------------------
// scs_serialize.js
// Copyright (c) 2006 
// Robert Umbehant
// rumbehant@wheresjames.com
// http://www.wheresjames.com
//
// Redistribution and use in source and binary forms, with or 
// without modification, are permitted for commercial and 
// non-commercial purposes, provided that the following 
// conditions are met:
//
// * Redistributions of source code must retain the above copyright 
//   notice, this list of conditions and the following disclaimer.
// * The names of the developers or contributors may not be used to 
//   endorse or promote products derived from this software without 
//   specific prior written permission.
//
//   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
//   CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
//   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
//   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
//   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
//   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
//   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
//   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
//   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
//   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
//   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
//   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
//   EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//----------------------------------------------------------------*/

function ScsSerialize( x_params )
{
	var ret = '';
	for ( var key in x_params )
	{
		if ( key && x_params[ key ] )
		{
			// Continue link
			if ( ret ) ret += ',';

			// Save the key
			ret += escape( key );

			if( x_params[ key ].constructor == Array ||
				x_params[ key ].constructor == Object )
			{
				ret += '{' + ScsSerialize( x_params[ key ] ) + '}'; 
			}
			else ret += '=' + escape( x_params[ key ] );

		} // end if
	}

	return ret;
}

function ScsDeserialize( x_params, x_arr )
{
	var l = x_params.length, s = 0, e = 0;

	while ( e < l )
	{
		switch( x_params[ e ] )
		{
			case ',' : case '}' :
			{
				var a = x_params.substr( s, e - s ).split( '=' );

				if ( 1 < e - s )
				{
					// Valid?
					if ( null == a[ 0 ] ) a[ 0 ] = 0;

					// Decode
					else a[ 0 ] = unescape( a[ 0 ] );

					// Single value?
					if ( null == a[ 1 ] ) x_arr[ 0 ] = '';

					// Key / value pair
					else x_arr[ a[ 0 ] ] = unescape( a[ 1 ] );

				} // end if

				// Next data
				s = e + 1;

				// Punt if end of array
				if ( '}' == x_params[ e ] ) return e + 1;

			} break;

			case '{' :
			{
				// Get the key
				var k = x_params.substr( s, e - s );

				if ( k.length )
				{
					// Decode the key
					k = unescape( k );

					// Decode array
					x_arr[ k ] = Array();
					e += ScsDeserialize( x_params.substr( e ), x_arr[ k ] );

				} // end if

				// Next data
				s = e + 1;

			} break;

		} // end switch

		// Next e
		e++;

	} // end while

	return e;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions