我试图使用Lift-Json自动将json对象反序列化为scala类,其中一个坐标类用于存储GeoJson信息.
case class Request(name:String, geometry:Geometry)
sealed abstract class Geometry
case class Point(coordinates:(Double,Double)) extends Geometry
case class LineString(coordinates:List[Point]) extends Geometry
case class Polygon(coordinates:List[LineString]) extends Geometry
Run Code Online (Sandbox Code Playgroud)
我想反序列化一个像这样的json字符串:
{
name:"test",
geometry:{
"type": "LineString",
"coordinates": [ [100.0, 0.0], [101.0, 1.0] ]
}
}
Run Code Online (Sandbox Code Playgroud)
在Geometry字段中使用正确的LineString运行时类进入Request case类.我想我应该使用TypeHint但是怎么样?这是正确的方法还是应该创建三个不同的Request(RequestPoint,RequestLineString和RequestPolygon)?这将是反序列化的Scala代码:
val json = parse(message)
json.extract[Request]
Run Code Online (Sandbox Code Playgroud) 你知道我怎样才能让一个对象只在一个特殊的类中变化?在这个例子中,我希望对象PrivateObject在类中只能变换(可递增)Box,而不是其他地方.有没有办法实现这个目标?
public class Box {
private PrivateObject prv;
public void setPrivateObject(PrivateObject p){
prv = p;
}
public void changeValue(){
prv.increment();
}
}
public class PrivateObject {
private value;
public increment(){
value++;
}
}
PrivateObject priv = new PrivateObject ();
Box box = new Box();
box.setPPrivateObject(priv);
box.changevalue();
priv.increment(); // I don't want it to be changeable outside the Box class!
Run Code Online (Sandbox Code Playgroud)
在C++中,我会将所有PrivateObject属性和方法Box设为私有,并将该类声明为该类的朋友PrivateObject.
我正在开发一个p2p聊天应用程序,它可以在DSL两个不同的NAT上正常工作,但是当涉及到3G USB互联网连接时,它就失败了.
我发现它不可能绕过3g网络的NAT,而Skype和种子等已知的p2p应用也无法绕过3g网络,每当遇到这些问题时,通过中央服务器发送数据.
我想知道3g网络的架构是什么.我听说他们没有私有IP,端口对只有公共IP,端口可用,一个公共端口可以分配给很多设备,我是否正确?如果是这样,服务器如何将数据发送到3g网络?
我是 Java 新手,也是这个网站的新手,所以如果错误很明显,我很抱歉,但我收到了一个错误,我不知道它意味着什么,我已尝试一切方法来修复它。
我目前正在编写一个基本的应用程序库,带有一些swing界面,但问题是当尝试创建书籍形式的窗口时,有相关代码。
这是主窗口。
public class VentanaPrincipal extends javax.swing.JFrame {
public VentanaPrincipal() {
initComponents();
this.setLocationRelativeTo(null);
}
private void bt_salirActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
EscribirBinario escritor = new EscribirBinario();
// Collections.sort(ListaClientes.clientes);
if (escritor.abrir(Main.archivo1)) {
for (int indice = 0; indice < ListaClientes.clientes.size(); indice++) {
escritor.escribir(ListaClientes.clientes.get(indice));
}
escritor.cerrar();
}
System.exit(0);
}
private void bt_clienteActionPerformed(java.awt.event.ActionEvent evt) {
MantenimientoCliente clientes = new MantenimientoCliente(this, true);
clientes.setVisible(true);
}
private void bt_libroActionPerformed(java.awt.event.ActionEvent evt) {
MantenimientoLibro book = new MantenimientoLibro(this, true);
book.setVisible(true); …Run Code Online (Sandbox Code Playgroud) 当我使用gcloud sdk在我的本地shell上运行一个简单的命令时.
$ kubectl get pod
Run Code Online (Sandbox Code Playgroud)
我收到这样的错误:
来自服务器的错误(禁止):禁止使用pod:用户"client"无法列出群集范围内的pod:未知用户"client"
相同的命令在GCP云shell上运行正常,并且输出
$ gcloud auth list
Run Code Online (Sandbox Code Playgroud)
如预期的那样:
Credentialed帐户
ACTIVE ACCOUNT
* foo@bar.com
我也尝试创建clusterrolebinding,但得到类似的错误.
我正在开展一个项目,要求我准确地控制用于绘制(粗略)圆形刺激的像素数量,尽管Bresenham的算法很棒,但它们并没有绘制任意区域的圆圈(据我所知) .我已经尝试过在绘制所需区域时中断Bresenham算法的脚本,但结果肯定是命中注定.有没有人知道使用给定数量的像素绘制"最佳"圆(有点主观,我知道)的方法?非常感谢!
ocamldebug中的调用堆栈是真正的调用堆栈,因此进行尾调用的函数不会显示在其中.这令人困惑.如何获得包含尾调用的回溯?
ocaml functional-programming stack-trace tail-call-optimization
我在想我们可以证明一个程序有错误.我们可以对其进行测试,以评估它是否或多或少具有抗虫性.
但有没有办法(甚至在理论上)证明程序没有错误?
对于简单的程序,例如"Hello World",我想我们应该能够做到.但是大型项目呢?
是否可以使用Play Framework 2.2在Scala中创建一个通用函数,它将任意对象序列化为JSON,而不必提供编写器或格式化程序?
例如,这个非通用代码将为Customer创建一个JSON响应:
import play.api.libs.json._
import play.api.libs.functional.syntax._
case class Customer(id: Int, name: String)
object scratch {
val p = Customer(1, "n")
//> p : Customer = Customer(1,n)
def createJsonResponseCustomer(data: Customer) = {
implicit val formatter = Json.format[Customer]
Json.obj("success" -> true, "data" -> Json.toJson[Customer](data))
}
createJsonResponseCustomer(p)
//> res0: play.api.libs.json.JsObject = {"success":true,"data":{"id":1,"name":"n"}}
}
Run Code Online (Sandbox Code Playgroud)
为了避免为每个不同的对象定义格式化程序,我想创建一个这样的通用函数:
def createJsonResponse[T](data: T) = {
implicit val formatter = Json.format[T]
Json.obj("success" -> true, "data" -> Json.toJson[T](data))
}
Run Code Online (Sandbox Code Playgroud)
但是,这种尝试产生错误No unapply function found的Json.format[T].
换句话说,这有效:
def …Run Code Online (Sandbox Code Playgroud) 我正在玩一些在线示例,其中一个有这样的短语:
do ... contents <- getDirectoryContents path `catch` const (return [])
Run Code Online (Sandbox Code Playgroud)
但它不会为我编译,并给出错误:
No instance for (Exception e0) arising from a use of `catch'
The type variable `e0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Note: there are several potential instances:
instance Exception NestedAtomically
-- Defined in `Control.Exception.Base'
instance Exception NoMethodError
-- Defined in `Control.Exception.Base'
instance Exception NonTermination
-- Defined in `Control.Exception.Base'
...plus 7 others
In a stmt of a 'do' block:
contents <- getDirectoryContents …Run Code Online (Sandbox Code Playgroud)