5

Actually, the problem is NOT how to do it, but if it's a design mistake. I'm worried because I've read a lot about using only standard data types in WS. However, I had no problem implementing one that receives a HashMap, and filling that parameter from PHP with nuSoap.

I have a ParameterBean class with this members (plus getters and setters of course), which includes a HashMap.

private int ID;
private String value;
private String name;
private HashMap<Integer, String> map = new HashMap<Integer, String>();

And a service that receives an instance from this class. Then from the PHP client I invoke:

$map = array(1 => 'Foo', 2 => 'Bar');
$paramsp = array(
    'ID' => '1',
    'value' => 'Some Value',
    'name' => 'A Name',
    'map' => $map
);
$params = array($paramsp);
$resp = $client->call('test',$params);
print_r($client->response);

It works like a charm! Question is: Is this frowned upon? Will this result in a headache in the future in some way?

nickb
  • 59,313
  • 13
  • 108
  • 143
Federico Cristina
  • 2,203
  • 1
  • 19
  • 37

1 Answers1

1

A HashMap is a pretty dang standard data type and should present no problems when used in a web service.

As you've seen, both PHP and Java have no problem dealing with hash maps. JSON supports them (though they're called "objects", and don't have the explicit typing of Java).

While truly esoteric datatypes might cause problems for web services that are expected to be widely interoperable, hash maps are not in that category and should be used without worry.

blahdiblah
  • 33,069
  • 21
  • 98
  • 152
  • Thanks for your answer. What would be a "truly esoteric datatype" then? – Federico Cristina Aug 01 '12 at 20:52
  • 1
    @FedericoCristina Things like tree or graph data structures certainly don't lend themselves to use in web services. By *truly* esoteric I was thinking more of things like [winged edges](http://en.wikipedia.org/wiki/Winged_edge) that have no natural representation in JSON/XML, and require non-trivial de/serialization for WS transmission. – blahdiblah Aug 01 '12 at 21:10