From ef6d8a21849a97cdfc63c5cb740dd55f24807a48 Mon Sep 17 00:00:00 2001 From: Mathias Date: Tue, 28 Oct 2014 13:23:35 +0100 Subject: Add member name unmangling to ProductFormats, fixes #120 --- src/main/scala/spray/json/ProductFormats.scala | 27 +++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'src/main') diff --git a/src/main/scala/spray/json/ProductFormats.scala b/src/main/scala/spray/json/ProductFormats.scala index 5f5a9f4..39d5300 100644 --- a/src/main/scala/spray/json/ProductFormats.scala +++ b/src/main/scala/spray/json/ProductFormats.scala @@ -78,12 +78,37 @@ trait ProductFormats extends ProductFormatsInstances { sys.error("Case class " + clazz.getName + " declares additional fields") if (fields.zip(copyDefaultMethods).exists { case (f, m) => f.getType != m.getReturnType }) sys.error("Cannot determine field order of case class " + clazz.getName) - fields.map(_.getName) + fields.map(f => ProductFormats.unmangle(f.getName)) } catch { case NonFatal(ex) => throw new RuntimeException("Cannot automatically determine case class field names and order " + "for '" + clazz.getName + "', please use the 'jsonFormat' overload with explicit field name specification", ex) } } + +} + +object ProductFormats { + private val operators = Map( + "$eq" -> "=", + "$greater" -> ">", + "$less" -> "<", + "$plus" -> "+", + "$minus" -> "-", + "$times" -> "*", + "$div" -> "/", + "$bang" -> "!", + "$at" -> "@", + "$hash" -> "#", + "$percent" -> "%", + "$up" -> "^", + "$amp" -> "&", + "$tilde" -> "~", + "$qmark" -> "?", + "$bar" -> "|") + + private def unmangle(name: String) = operators.foldLeft(name) { case (n, (mangled, unmangled)) => + if (n.indexOf(mangled) >= 0) n.replace(mangled, unmangled) else n + } } /** -- cgit v1.2.3