//////////////////////////////////////////////////////////////////////////////////
//
//	JSON
//	06.10 2008 by  Veikko Salminen
//
//////////////////////////////////////////////////////////////////////////////////
//
//	tukee stringejä numeroita ja sisäisiä objekteja
//  {testi:"terve",0:1,1:2,objekti:{sisässä:2}}

var char_counter;

function convert_json_to_array(json) {
	
	char_counter = 0;
	return converter(json);
}

function converter(json) {
	var next = "", name, value;
	var temp = Array();
	
	//alert(json);
	
	if (json == "") return null;
	
	if (json.substr(char_counter,1) != "{") return "error";
	
	if (json.substr(char_counter+1,1) == "}") {
		char_counter++;
		return null;
	}
		
	while (json.substr(char_counter,1) != "}" || char_counter == 0) {
		
		char_counter++;
		name = read_name(json);
		
		c = json.substr(char_counter, 1);
		if (c != ":") return "error";
		char_counter++;
		
		value = read_value(json);
				
		temp[name] = value;
	}
  
  return temp;	
}

function read_name(json) {
	
	var c, temp = "";
	
	while (json.substr(char_counter,1) != ":" && char_counter <= json.length) {
		 c = json.substr(char_counter,1);
		 temp = temp + c;
		 char_counter++;
  }
  return temp;
}

function read_value(json) {
	
	var c, temp = "", stop = false, is_string = false;
	
	// jos value alkaa "-merkillä on kysessä stringi
	if (json.substr(char_counter,1) == "\"") {
		is_string = true;
		char_counter++;  // hypätään ensimmäisen "-merkin yli
	}
	// jos value alkaa {-merkillä on kyseessä sisäinen objekti
	else if (json.substr(char_counter,1) == "{") {
		temp = converter(json);
		char_counter++; // hypätään lopetus }-merkin yli
		return temp;
	}
	
	while ((json.substr(char_counter,1) != "," && json.substr(char_counter,1) != "}" && char_counter <= json.length && !is_string) ||
					(json.substr(char_counter,1) != "\"" && char_counter <= json.length && is_string)) {
		c = json.substr(char_counter,1);
		temp = temp + c;
		char_counter++;
  }
  
  if (!is_string) {
  	if (temp == "") return "";
  	temp = parseInt(temp);
  }
  else
  	char_counter++;				//  hypätään lopussa olevan "-merkin yli
  
  return temp;
}

function convert_array_to_json(arr) {
		
	var temp = "{";
	var first = true;
	
	// käydään läpi koko taulukko
	for(var obj in arr){
		 b = typeof arr[obj];
		 
		 //alert("type="+b + " arr="+arr[obj] + " as int = " +parseInt(arr[obj])+" == "+(parseInt(arr[obj]) != arr[obj] && arr[obj] != ''));

		 // sisäiset objektit luodaan rekursiolla
		 if (b == "object") {
		   value = convert_array_to_json(arr[obj]);
		 }
		 //  stringin tunnistus
		 else if (parseInt(arr[obj]) != arr[obj] && arr[obj] != '' && arr[obj]) {
		 	 value = arr[obj];
		 	 var temp_value = "";
		 	 // muutetaan "-merkit '-merkeiksi
		 	 for (var i=0;i < value.length; i++)
		 	 	 temp_value = temp_value + (value.substr(i,1) == "\"" ? "'" : value.substr(i,1))  
		 	 value = "\""+temp_value+"\"";
		 }
		 else {
		 	 value = arr[obj];
		 }
		 
		 if (!first) temp = temp + ",";
		 temp = temp+ obj+':'+value;
		 
		 first = false;
	}
	
	temp = temp + "}";
	
	return temp;	
}